Sha2b Sha2b - 25 days ago 14
Python Question

Keras in `pip list` and anaconda package list but cannot import

I am using anaconda 4.4.0 with Python 3.6.2. First, I pip installed Keras, it shows on

pip list
and
conda list
and also in
environment>root>packages
in Anaconda. But when I use
import keras
,in Anaconda or in Terminal, I get


Traceback (most recent call last):
...
ModuleNotFoundError: No module named 'keras'


I tried installing Keras with anaconda also, and I get the same
ModuleNotFoundError
.

I have also tried uninstalling it, then download the master branch from github and install using
python setup.py install
. Again the package is in the lists but I cannot import it.

UPDATE:

I have uninstalled packages several times and installed using Keras package in pypi, Now I am able to import the packages in the python in terminal. But still I cannot import in jupyter notebook.

It may worth mentioning that I am working with MacOS Sierra 10.

Answer Source

If you are able to import in shell but not in the notebook, according to gnestor's answer to this question, your python may use different executables. Run:

import sys
sys.executable

in shell and in notebook. If the directories are different then you must change the kernelspec of the notebook. This might have happened by installing Tensorflow or other packages that change the environment and kernelspec. You can find the kernelspec directory with this command:

from jupyter_core.paths import jupyter_data_dir
print(jupyter_data_dir())

In that directory you will find a JSON file. Open it and change the path to your working python directory. (working python directory in which python)