python - 如何修复“导入 keras 不工作”错误
问题描述
我已经远程使用过 Keras,例如在 Colab 和 AWS 上。今天,我尝试在我的笔记本电脑上使用 pip3 install keras 安装 Keras,消息是计算机上已经安装了 keras(我之前肯定已经安装过):
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Requirement already satisfied: keras in /usr/local/lib/python3.7/site-packages/Keras-2.4.3-py3.7.egg (2.4.3)
Requirement already satisfied: h5py in /usr/local/lib/python3.7/site-packages/h5py-2.10.0-py3.7-macosx-10.15-x86_64.egg (from keras) (2.10.0)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.7/site-packages (from keras) (1.16.4)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/site-packages/PyYAML-5.3.1-py3.7-macosx-10.15-x86_64.egg (from keras) (5.3.1)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.7/site-packages (from keras) (1.2.0)
Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from h5py->keras) (1.12.0)
但是,当我尝试这样做时import keras
,我收到了以下消息:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-88d96843a926>", line 1, in <module>
import keras
ModuleNotFoundError: No module named 'keras'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2018, in showtraceback
stb = value._render_traceback_()
AttributeError: 'ModuleNotFoundError' object has no attribute '_render_traceback_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 1095, in get_records
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 313, in wrapped
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 347, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 1502, in getinnerframes
frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 1460, in getframeinfo
filename = getsourcefile(frame) or getfile(frame)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 696, in getsourcefile
if getattr(getmodule(object, filename), '__loader__', None) is not None:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 733, in getmodule
if ismodule(module) and hasattr(module, '__file__'):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/__init__.py", line 50, in __getattr__
module = self._load()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "/Library/Frameworks/Python.framework/Versions/3.7/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 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/__init__.py", line 32, in <module>
from tensorflow._api.v1 import app
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/_api/v1/app/__init__.py", line 10, in <module>
from tensorflow.python.platform.app import run
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 23, in <module>
from absl.app import run as _run
ModuleNotFoundError: No module named 'absl'
我用谷歌搜索了类似的问题,并找到了一些尝试的建议sudo pip3 install keras
。Keras又被下载了。但是,当我输入 时import keras
,它仍然产生错误。
另外,我阅读了这个 Stack Overflow 问题:How to fix import keras error with python console,但该解决方案对我不起作用,因为我正在调用一个调用的库import keras
,所以我需要该import keras
命令才能工作(我无法做到打电话import tensorflow.keras
)。
为什么会发生此错误?
注意:我使用的是 Python 3.7.5。我检查了 pip through 的版本pip --version
,收到以下消息:
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
另外,我正在 Jupyter Notebook 中运行此代码。我检查了 Python 3.7 正在由 Jupyter Notebook 运行。
解决方案
确保您使用python3
. Mac 和其他一些计算机默认安装了 Python 2.7 python
,这每天都会让你感到困惑。
如果这不起作用,请设置一个虚拟环境:在 virtualenv 中使用 Python 3
推荐阅读
- android - 主线程无法加载所有 RecyclerView 项目。最好的方法是什么?
- python - 如何递归地将 HTML 映射到 Python 字典?
- python - 如何提高 find_elements_by_xpath 的速度
- api - Tomcat API:当客户端在每次请求后关闭连接时如何提高性能?
- r - 如何制作列值是变量的频率表?
- json - 如何在 Flutter 中从 json 自动生成通用响应模型
- python - 重新格式化 json 文件(python)
- python - 尝试使用熊猫从网站中提取 html 表
- python - 文档中的示例在 Jupiter Notebook 中不起作用
- git - VSCode:查看整个 git commit diff