Jupyter can't find keras' module

2020-04-03 06:05发布

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'.

I have tried to solve this issue by 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?

8条回答
2楼-- · 2020-04-03 06:45

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.

查看更多
我只想做你的唯一
3楼-- · 2020-04-03 06:52

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.

查看更多
姐就是有狂的资本
4楼-- · 2020-04-03 07:00

Please try the following:

Run these in the jupyter notebook cell:

import sys

sys.path

sys.executable

It may not be pointing to your virtual environment but to the root

The fix is to install the jupyter notebook from inside your virtual environment

$ . your_env/bin/activate

(your_env)$ python -m pip install jupyter

Now you can import tensorflow or keras

查看更多
霸刀☆藐视天下
5楼-- · 2020-04-03 07:00

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:

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.

The step to fix is simply re-activate my environment, with:

source deactivate && source activate [my_env]

Then I am using a correct 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']
查看更多
戒情不戒烟
6楼-- · 2020-04-03 07:00

(Not an answer but some troubleshooting hints)

sys.path is not the path to your Python executable, but the path to the libraries.

  • 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?

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.

查看更多
Animai°情兽
7楼-- · 2020-04-03 07:00

The kernel in console and jupyter are not necessarily the same, and the problem might be that you are not on 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

Using both Python 2.x and Python 3.x in IPython Notebook

was very helpful.

查看更多
登录 后发表回答