任意版本Spyder无法导入TensorFlow 2.x:tensorflow_core.estimator报错问题
我为不同版本的TensorFlow和PyTorch分别创建了独立的conda环境,在终端直接调用Python运行时所有环境均无异常。
我也可以通过两种方式在Spyder中使用这些环境:一是在每个conda环境中单独安装Spyder,二是采用官方说明的「模块化方案」。
但只要使用TensorFlow 2.x版本,在Spyder中导入TensorFlow的所有尝试都会失败,始终提示无法导入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'
该问题仅出现在TensorFlow 2(2.0、2.1版本)和Spyder(3、4、5版本)搭配使用的场景中。
我目前尚未找到可行的解决方案,也不清楚为什么Spyder导入TensorFlow时会尝试加载tensorflow_core.estimator模块,以及为什么在终端用Python导入TensorFlow不会触发该报错。这并非环境配置问题:该环境中的所有其他模块都可以在Spyder中正常导入。
和该模块相关的问题并不少见,但我暂未找到有效的解决方法。
内容的提问来源于stack exchange,提问作者hirschme
相关产品推荐
相关产品推荐

