Jupyter can't find keras' module

2020-04-03 06:54发布

问题:

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?

回答1:

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



回答2:

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


回答3:

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



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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'



回答8:

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.

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.