无法在 Jupyter 中导入 Keras

Nir*_*han 1 python virtualenv jupyter keras jupyter-notebook

所以我使用 python 3 和 jupyter (使用 pip)设置了虚拟局域网。一切正常,但如果我尝试导入 keras,它将无法在 jupyter 中工作并告诉模块未找到。但是如果我在终端中执行相同的文件(python3 test.py),它就可以正常工作。

which jupyter
/usr/local/bin/jupyter

which python3
/Users/niro273/Desktop/xcorp/bin/python3
Run Code Online (Sandbox Code Playgroud)

如果我这样做,pip3 list这些就是结果。

jupyter (1.0.0)
jupyter-client (5.1.0)
jupyter-console (5.2.0)
jupyter-core (4.3.0)
Keras (2.0.8)
Run Code Online (Sandbox Code Playgroud)

注意-我也在虚拟环境中安装了 jupyter (pip3 install jupyter)。那么我是否必须切换 juypter 执行路径?将不胜感激任何指导。

des*_*aut 5

Keras 和 Jupyter 都必须安装在您的虚拟环境中;然后,您应该在激活虚拟环境后启动 Jupyter(在这种情况下which jupyter应该指向虚拟环境中的不同位置):

$ which jupyter
/usr/local/bin/jupyter
$ virtualenv /venv/foo
$ source /venv/foo/bin/activate
$ (foo) pip3 install jupyter
$ (foo) which jupyter
/venv/foo/bin/jupyter
$ (foo) pip3 install keras
$ (foo) jupyter notebook
Run Code Online (Sandbox Code Playgroud)

当然还有其他方法(例如,在主 Jupyter 安装中安装不同的内核,指向foo虚拟环境中的 Python 可执行文件),但我发现上述方法更快、更轻松,至少对于 Keras 来说是这样。 。