I keep on receiving this error:
Traceback (most recent call last) File "tensorflow.py", line 1, in <module> import tensorflow as tf File "C:UsersAnushDesktoptensorflow.py", line 2, in <module> print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) AttributeError: partially initialized module 'tensorflow' has no attribute 'config' (most likely due to a circular import)
Here is my code:
import tensorflow as tf print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
Here are my Tensorflow versions: tensorboard-2.3.0 tensorflow-2.3.1 tensorflow-estimator-2.3.0 tensorflow-gpu-2.3.1
I am on Python 3.8.0
Advertisement
Answer
When python executes a script, it includes the script’s directory in the python path. This makes it easy for programmers to supply their own modules for the script without an explicit install. Also, python doesn’t treat that top level script as a module (or more precisely, it names it __main__
).
In your case, you named your script “tensorflow.py”. Python executes the script and when it sees import tensorflow
, it imports your module, not the real tensorflow
package. Since it doesn’t have a module named tensorflow
yet (the original is __main__
), it executes the file again and sees that import tensorflow
a second time, but again, its still your same tensorflow.py
. That could go on forever, but python detects the circular import and emits the error you see.
Its too bad that python does this. It makes it easier to deploy modules and packages but its kind of fragile. You have to make sure that your .py file names don’t clash with anything that may show up in your python installation.
The solution is to rename C:UsersAnushDesktoptensorflow.py
. And just don’t name it the same as any python package you may install.