Reputation: 894
I have different conda environments for different tensorflow and pytorch versions. They all work without issues when calling python on the terminal.
I can also use them with Spyder, either with an individual Spyder installation in each conda environment, or using the "modular approach" described here: https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder
BUT, every attempt to import tensorflow with spyder fails when dealing with tensorflow 2.x , always with the same error of failing to import the module tensorflow_core.estimator
Error in callback <bound method AutoreloadMagics.post_execute_hook of <autoreload.AutoreloadMagics object at 0x7f41b8711990>> (for post_execute):
Traceback (most recent call last):
File "/home/user/anaconda3/envs/tf2_env/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 538, in post_execute_hook
_, pymtime = self._reloader.filename_and_mtime(sys.modules[modname])
File "/home/user/anaconda3/envs/tf2_env/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 184, in filename_and_mtime
if not hasattr(module, '__file__') or module.__file__ is None:
File "/home/user/anaconda3/envs/tf2_env/lib/python3.7/site-packages/tensorflow/__init__.py", line 50, in __getattr__
module = self._load()
File "/home/user/anaconda3/envs/tf2_env/lib/python3.7/site-packages/tensorflow/__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "/home/user/anaconda3/envs/tf2_env/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tensorflow_core.estimator'
This issue is particular for tensorflow 2 (2.0 and 2.1) and Spyder (version 3, 4 and 5).
I haven't been able to find a way of making this work, and I have no idea why Spyder tries to load the module tensorflow_core.estimator
when importing tensorflow, or why this is not an error when importing tensorflow with python from the terminal. This is NOT an environment problem: All other modules in the environment are present and can be imported with Spyder.
Issues with this module are not uncommon, but I haven't seen a single solution:
ModuleNotFoundError: No module named 'tensorflow_core.estimator' for tensorflow 2.1.0
https://github.com/tensorflow/tensorflow/issues/34607
https://www.reddit.com/r/tensorflow/comments/jesmaq/how_to_resolve_no_module_named_tensorflow/
https://nomodulenamed.com/m/tensorflow_core.python.autograph.core.config_lib
Upvotes: 0
Views: 720
Reputation: 894
SOLVED: There was a version mismatch between tensorflow-gpu and tensorflow-estimator
When installing tensorflow-gpu=2.x , for some reason, a mismatching tensorflow-estimator version was being downloaded. For tensorflow-gpu=2.1, tensorflow-estimator=2.4 was being downloaded. Downgrading this to tensorflow-estimator=2.1 solved the import issue in Spyder.
Why Spyder imports tensorflow-estimator whereas Python itself doesn't is still unresolved. And why this version mismatch is happening after tensorflow-gpu=2.x is also unresolved.
Upvotes: 2
Reputation: 784
Uninstall everything tensorflow-related.
$ pip uninstall tensorflow-estimator \
tensorboard tb-nightly tf-estimator-nightly
Then install the packages again. This usually fixes it.
Source: https://github.com/tensorflow/tensorflow/issues/32952
Edit: If you have installed Spyder from the Anaconda, go to the Anaconda launcher. There go to environments, you will see two of them: root and tensorflow. The latter one is created due to the instructions by tensorflow.org. Just run all those instructions on the root, don't activate tensorflow environment, it will work. Everything will be available in spyder.
Upvotes: 1