Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AttributeError] module 'wandb' has no attribute 'init' #5950

Closed
hugo4777 opened this issue Jul 25, 2023 · 12 comments
Closed

[AttributeError] module 'wandb' has no attribute 'init' #5950

hugo4777 opened this issue Jul 25, 2023 · 12 comments

Comments

@hugo4777
Copy link

  I try to search on the Internet ,ask chat GPT and turn to my friend for help but didn‘t solve the problem. 

install the 0.15.6 version of wandb in my anaconda python environment , and successfully login my account. Then i wrote a simple test code using wandb. everything was fine, i got the plot.
Question: But when I added wandb code to my project code , An attribute Error was reported.

  1. I did import the package wandb and write wandb.init(project="project_name") in the right place.
  2. Also I can find the function init() in wandb code.
  3. The version of wandb I'm using doesn't conflict with other dependent packages(tensorflow==1.14.0,gym==18.3,torch==1.10.0)
  4. The sample code given in the official documentation is available

this is the code that reported error
`from .Enterprise import Enterprise
from .Bank import Bank
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import os
import time
import shutil
from pandas import DataFrame
import wandb

wandb.init(project="cortex22_anna")

plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题
base_path_default = 'E:\run'

class Logger:{...}
`

@eliphatfs
Copy link

eliphatfs commented Jul 25, 2023

My guess is that you have either:

  1. a wandb directory that contains an __init__.py, or
  2. a wandb.py file in your working directory.

These would cause the import to resolve to the local files instead of the installed wandb. If that is the case, you will have to rename or remove these files or folders in the local working directory.

@hugo4777
Copy link
Author

My guess is that you have either:

  1. a wandb directory that contains an __init__.py, or
  2. a wandb.py file in your working directory.

These would cause the import to resolve to the local files instead of the installed wandb. If that is the case, you will have to rename or remove these files or folders in the local working directory.

thanks for your kind help

  • I do have a a wandb directory that contains an init.py ,this is the path of the init.py. C:\Users\lyk.conda\envs\sevenpython36\Lib\site-packages\wandb . I try to remove it but still has error.

  • my working directory only has one wandb directory contains some running logs.

  • Also I find when I run a single python file are fine ,but This problem only occurs when my project code has referential relationship. Such as I wrote it in Logger.py and used in environment.py

@hugo4777
Copy link
Author

I did another test , it proves code has referential relationship dose not cause the error . crazy!!!!

My guess is that you have either:

  1. a wandb directory that contains an __init__.py, or
  2. a wandb.py file in your working directory.

These would cause the import to resolve to the local files instead of the installed wandb. If that is the case, you will have to rename or remove these files or folders in the local working directory.

thanks for your kind help

  • I do have a a wandb directory that contains an init.py ,this is the path of the init.py. C:\Users\lyk.conda\envs\sevenpython36\Lib\site-packages\wandb . I try to remove it but still has error.
  • my working directory only has one wandb directory contains some running logs.
  • Also I find when I run a single python file are fine ,but This problem only occurs when my project code has referential relationship. Such as I wrote it in Logger.py and used in environment.py

I did another test , it proves code has referential relationship dose not cause the error . crazy!!!!

@eliphatfs
Copy link

I am sorry I did not make it clear. Do not remove the __init.py__ file in that wandb directory. If you did that, you will have to uninstall and reinstall wandb.
I only meant to delete those in your working directory, if they exist.
You may run print(wandb.__file__) before wandb.init to see what files are interfering with the process.

@hugo4777
Copy link
Author

I am sorry I did not make it clear. Do not remove the __init.py__ file in that wandb directory. If you did that, you will have to uninstall and reinstall wandb. I only meant to delete those in your working directory, if they exist. You may run print(wandb.__file__) before wandb.init to see what files are interfering with the process.

Thank you so much.
I try to run the code you gave but in the project code it reported an AttributeError: module 'wandb' has no attribute '__file__' .
I also try it in another useable test code and the result are correct C:\Users\lyk\.conda\envs\sevenpython36\lib\site-packages\wandb\__init__.py
P.S. both these two codes are in the same conda environment

@eliphatfs
Copy link

Then I guess the python you used in your project code is not finding wandb correctly.

You may try printing __path__ attribute of wandb to confirm. However, I cannot know the root cause now.

You may try moving the wandb directory that contains run files inside the working directory to somewhere else and try again.

If that works, it is a name resolution order issue; if that did not work with 'no module named wandb', then the path to conda is corrupted in your project. In either case you should check for files that modified sys.path and fix them to fix the problem completely.

If that still fails with the same AttributeError, and printing __path__ also fails, I recommend you posting a minimal version of your project here for reproducing the problem.

@hugo4777
Copy link
Author

Then I guess the python you used in your project code is not finding wandb correctly.

You may try printing __path__ attribute of wandb to confirm. However, I cannot know the root cause now.

You may try moving the wandb directory that contains run files inside the working directory to somewhere else and try again.

If that works, it is a name resolution order issue; if that did not work with 'no module named wandb', then the path to conda is corrupted in your project. In either case you should check for files that modified sys.path and fix them to fix the problem completely.

If that still fails with the same AttributeError, and printing __path__ also fails, I recommend you posting a minimal version of your project here for reproducing the problem.

I tried everything you said,

  1. the path of wandb in my working directory is: ['C:\\Users\\\lyk\\\Desktop\\\\\Cortex22_Anna\\real_System_remake\\\wandb'],
  2. and after I moved the directory where run files are located in wandb, it says no module named wandb,
  3. then I checked sys.path and found that there is a problem with the path and added the path of wandb in my conda environment: C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages/wandb", then the error is still exist

Sadly , my project is so coupled it's hard to take out the smallest part of the instances.

Anyway thanks so much for your help. As much as this bug is killing me, I feel very lucky to have your answer.

P.S.
this is part of my sys.path

path = [
    'F:\\pycharm\\PyCharm 2022.1.1\\plugins\\python\\helpers',
    'F:\\pycharm\\PyCharm 2022.1.1\\plugins\\python\\helpers\\generator3',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36\\python36.zip',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36\\DLLs',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36\\lib',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36\\lib\\site-packages',
    'C:\\Users\\lyk\\.conda\\envs\\sevenpython36\\lib\\site-packages\\wandb',
    'C:/Users/lyk/.conda/envs/sevenpython36/DLLs',
    'C:/Users/lyk/.conda/envs/sevenpython36/Lib',
    'C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages',
    'C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages/wandb'
]

@eliphatfs
Copy link

Did you print that sys.path just before the wandb.init that is causing the error? If not please do so.

@hugo4777
Copy link
Author

Did you print that sys.path just before the wandb.init that is causing the error? If not please do so.

I do print the sys.path just before the wandb.init

import wandb
import sys
print(sys.path)
wandb.init(project="cortex22_anna")

this is the result:

AttributeError: module 'wandb' has no attribute 'init'
['C:\\Users\\lyk\\Desktop\\Cortex22_Anna\\real_System_remake', 'C:\\Users\\lyk\\Desktop\\Cortex22_Anna', 'F:\\python\\python36.zip', 'F:\\python\\DLLs', 'F:\\python\\lib', 'F:\\python', 'F:\\python\\lib\\site-packages']

@eliphatfs
Copy link

See, you are missing C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages (where wandb is located).
Most probably you are actually activating a wrong conda environment somewhere.
Or perhaps you have some code that wrongly modifies sys.path, or installed a library that does it wrongly.

You should fix these root causes. If you still cannot figure it out though, a workaround could be:

import sys
sys.path.insert(0, 'C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages')
import wandb
wandb.init(project="cortex22_anna")

@hugo4777
Copy link
Author

See, you are missing C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages (where wandb is located). Most probably you are actually activating a wrong conda environment somewhere. Or perhaps you have some code that wrongly modifies sys.path, or installed a library that does it wrongly.

You should fix these root causes. If you still cannot figure it out though, a workaround could be:

import sys
sys.path.insert(0, 'C:/Users/lyk/.conda/envs/sevenpython36/Lib/site-packages')
import wandb
wandb.init(project="cortex22_anna")

OMG, I think I found the difference between the correct code and the error code sys.path, in the test code sys.path is correct, but in the error code the path is really the local python environment path. 'F:\python\python36.zip', 'F:\python\DLLs', 'F:\python\lib', 'F:\python', 'F:\python\lib\site-packages']

I find that though my pycharm environment is set up as my conda environment but my project code 's run configuration has been set up as local python interpreter!!
Now it is working!!
thank you so much!!
very happy to met you !!
have a good day!

@kptkin
Copy link
Contributor

kptkin commented Dec 19, 2023

@eliphatfs Thanks a lot for your help!
@hugo4777 Glad to see that your issue is resolved closing this issue!

@kptkin kptkin closed this as completed Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants