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?
You have to do !pip install keras within your jupyter notebook to install the keras package before you can import keras. Keras uses tensorflow backend, so when you install keras it installs tensorflow as part of the requirements.
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
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With