Python Jupyter 找不到 keras 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43557881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jupyter can't find keras' module
提问by Simone
I have installed Tensorflow and Keras by Anaconda (on Windows 10), I have created an environment where I am using Python 3.5.2 (the original one in Anaconda was Python 3.6).
When I try to execute import keras as ks
, I get ModuleNotFoundError: No module named 'keras'
.
我已经安装了 Anaconda 的 Tensorflow 和 Keras(在 Windows 10 上),我创建了一个使用 Python 3.5.2 的环境(Anaconda 中的原始环境是 Python 3.6)。当我尝试执行时import keras as ks
,我得到ModuleNotFoundError: No module named 'keras'
.
I have tried to solve this issue by sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)
我试图通过 sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)
with both notebook and console, but I continue to get the same error.
使用笔记本和控制台,但我继续遇到相同的错误。
How could I solve this issue?
我怎么能解决这个问题?
回答by sandeep srivastava
Please try the following:
请尝试以下操作:
Run these in the jupyter notebook cell:
在 jupyter notebook 单元中运行这些:
import sys
sys.path
sys.executable
It may not be pointing to your virtual environment but to the root
它可能不是指向您的虚拟环境,而是指向根
The fix is toinstall the jupyter notebook from inside your virtual environment
修复方法是从虚拟环境中安装 jupyter notebook
$ . your_env/bin/activate
(your_env)$ python -m pip install jupyter
Now you can import tensorflow or keras
现在您可以导入 tensorflow 或 keras
回答by bizi
Jupyter uses iPython under the hood, for python. So when you install Jupyter, it will also install iPython. There was one issue when I installed keras and Jupyter: I already have iPython installed in my root Anaconda environment. This is the output after I install Jupyter and keras:
Jupyter 在底层使用 iPython,用于 Python。所以当你安装 Jupyter 时,它也会安装 iPython。我在安装 keras 和 Jupyter 时遇到了一个问题:我已经在我的根 Anaconda 环境中安装了 iPython。这是我安装 Jupyter 和 keras 后的输出:
In [2]: import sys; sys.path
Out[2]:
['/home/user/anaconda3/bin',
'/home/user/anaconda3/lib/python36.zip',
'/home/user/anaconda3/lib/python3.6',
'/home/user/.ipython']
Notice that even though I am inside my conda environment, it still looks for libraries in my root conda environment. And of course keras isn't there.
请注意,即使我在 conda 环境中,它仍会在我的根 conda 环境中查找库。当然,keras 不在那里。
The step to fix is simply re-activate my environment, with:
修复步骤只是重新激活我的环境,使用:
source deactivate && source activate [my_env]
source deactivate && source activate [my_env]
Then I am using a correct ipython:
然后我使用了正确的 ipython:
Out[2]:
['/home/user/anaconda3/envs/ml3/bin',
'/home/user/anaconda3/envs/ml3/lib/python36.zip',
'/home/user/anaconda3/envs/ml3/lib/python3.6',
'/home/user/.ipython']
回答by Ashalynd
(Not an answer but some troubleshooting hints)
(不是答案,而是一些故障排除提示)
sys.path is not the path to your Python executable, but the path to the libraries.
sys.path 不是 Python 可执行文件的路径,而是库的路径。
- Check where Keras is installed and check your sys.path. How exactly did you install Keras?
- Try to execute the same command from the Python interpreter. Do you have the same issue?
- How did you install Jupiter, is the sys.path visible from there the same as sys.path visible from your Python interpreter?
- Do Jupiter and Keras use the same version of Python?
- 检查 Keras 的安装位置并检查您的 sys.path。您究竟是如何安装 Keras 的?
- 尝试从 Python 解释器执行相同的命令。你有同样的问题吗?
- 您是如何安装 Jupiter 的,从那里可见的 sys.path 是否与从 Python 解释器可见的 sys.path 相同?
- Jupiter 和 Keras 使用相同版本的 Python 吗?
You might try to uninstall Jupiter and install it again, and hope that the new installation picks up the packages which are already installed. What could happen is that you have more than one Python installation and different libraries being installed to the different places. sys.path, when requested from different environments, might give you a hint if that's true.
您可能会尝试卸载 Jupiter 并重新安装它,并希望新安装能够获取已安装的软件包。可能发生的情况是,您安装了多个 Python 并且将不同的库安装到不同的位置。sys.path,当从不同环境请求时,可能会给你一个提示,如果这是真的。
回答by pgrenholm
The kernel in console and jupyter are not necessarily the same, and the problem might be that you are not on python 3.5.
控制台和 jupyter 中的内核不一定相同,问题可能是您不在 python 3.5 上。
python --version
should tell you what is running in the console, and in jupyter you should see it as a choice on starting a new notebook. For me, the information in
应该告诉您控制台中正在运行什么,而在 jupyter 中,您应该将其视为启动新笔记本的选择。对我来说,信息在
Using both Python 2.x and Python 3.x in IPython Notebook
在 IPython Notebook 中同时使用 Python 2.x 和 Python 3.x
was very helpful.
非常有帮助。
回答by Simone
I have realized that I had two different Jupyter's directories, so I have manually deleted one on them. Finally, I have reinstalled Anaconda. Now Keras works properly.
我意识到我有两个不同的 Jupyter 目录,所以我手动删除了其中一个。最后,我重新安装了Anaconda。现在 Keras 工作正常。
回答by srilalitha
If you are a windows/mac user who are working on Jupyter notebook “pip install keras” doesn't help you .Try the below steps.It was solved for me 1. In command prompt navigate to the “site packages” directory of your anaconda installed. 2. Now use “conda install tensorflow” and after “conda install keras” 3. Re-start your Jupyter notebook and run the packages.
如果您是使用 Jupyter 笔记本的 windows/mac 用户,“pip install keras”对您没有帮助。请尝试以下步骤。它已为我解决 1. 在命令提示符下导航到您的“站点包”目录蟒蛇安装。2. 现在使用“conda install tensorflow”,然后使用“conda install keras” 3. 重新启动你的 Jupyter notebook 并运行这些包。
回答by Abhishek Agarwal
Acually, I did this command pip install keras
and sudo -H pip3 install keras
and pip3 install keras
. None of them worked. I added the following command and everything worked like a charm:
pip install Keras
. Yes a capital 'K'
Acually,我做了这个命令pip install keras
和sudo -H pip3 install keras
和pip3 install keras
。他们都没有工作。我添加了以下命令,一切都像一个魅力:
pip install Keras
. 是大写的“K”
回答by GMA
Here's how I solved this problem.
这是我解决这个问题的方法。
First, the diagnosis. When I run which python
in a terminal window on my Mac (the same terminal I used to launch jupyter
, I get /Users/myusername/.conda/envs/myenvname/bin/python
, but when I run the same command from a terminal within Jupyter, I get /usr/bin/python
. So Jupyter isn't using the correct python executable; the version it's using doesn't have any of my packages installed.
第一,诊断。当我运行which python
在我的Mac上一个终端窗口(用于启动同一个终端我jupyter
,我得到的/Users/myusername/.conda/envs/myenvname/bin/python
,但是当我从终端运行相同的命令中Jupyter,我得到/usr/bin/python
那么Jupyter没有使用正确的Python可执行文件;该它使用的版本没有安装我的任何软件包。
But which jupyter
returns /usr/bin/jupyter
; it's using a version of jupyter that isn't coming from within my conda environment. I ran conda install jupyter
and now which jupyter
returns /Users/myusername/.conda/envs/myenvname/bin/jupyter
(for some reason I had to restart the terminal window for this to take effect.) Then if I relaunch jupyter notebook
, the notebook is using the correct version of Python and I have access to all my installed conda packages.
但which jupyter
返回/usr/bin/jupyter
;它使用的 jupyter 版本不是来自我的 conda 环境。我跑了conda install jupyter
,现在which jupyter
返回/Users/myusername/.conda/envs/myenvname/bin/jupyter
(出于某种原因,我必须重新启动终端窗口才能生效。)然后,如果我重新启动jupyter notebook
,则笔记本使用的是正确版本的 Python,并且我可以访问所有已安装的 conda 包。