Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conda installed packages not working with jupyter

Tags:

I had installed tensorflow with pip install, and installed keras with conda install: (my python3 is installed under Anaconda3)

conda create -n keras python=3.5
activate keras
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl
conda install --channel https://conda.anaconda.org/conda-forge keras

Now when I run the following commands in commandline, everything works fine:

activate keras
python
import tensorflow
import keras
from keras.datasets import mnist

However, when I run the same commands with jupyter, I get an error on the keras import line:

activate keras
jupyter notebook
# now open a python3 notebook
# and enter the above commands in it
# and run

ImportError: No module named 'keras'

However the import for tensorflow gives no error.

I then uninstalled tensorflow, and reinstalled it with conda install:

conda install tensorflow

Now when I run jupyter, I get the same error on the tensorflow line also.

How can I get jupyter to work on packages installed with conda?

Additional information: I ran sys.executable both from commandline and jupyter, and both point to the same path:

'C:\\sw\\Anaconda3\\envs\\tensorflow\\python.exe'

It looks like a recent problem, I found a similar report at https://github.com/jupyter/jupyter/issues/245, But I did not quite understand the solution. Can anyone help here?

like image 342
R71 Avatar asked Aug 15 '17 13:08

R71


People also ask

Can I use conda install in Jupyter Notebook?

Install Jupyter Notebook / Lab in the base environmentJupyter Notebook can easily be installed using conda.

Does pip install work in Jupyter?

Globally installing packages: So, pip installs the package in the currently-running Jupyter kernel. This is done to overcome the disconnectedness between Jupyter kernels and Jupyter's Shell, i.e., the installer points to a different Python version than the one being used in the notebook.


1 Answers

I found the answer at http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments

ipykernel has to be linked to the environment, and then jupyter can use it.

The following installation procedure works:

conda create -n keras python=3.5 ipykernel
activate keras
python -m ipykernel install --user --name keras
jupyter notebook

Now if I call sys.executable in the jupyter notebook, it prints the correct environment from where the executables are accessed.

like image 147
R71 Avatar answered Oct 11 '22 17:10

R71