简体   繁体   中英

jupyter notebook not using python in conda environment from which it was started

I had started with udacity deep learning course and was setting up environments. I think the kernel notebook uses does not use python from conda environment. Following are some of the results of things I have tried.

Started conda environment

source activate tensorflow

With python terminal inside conda environment from linux terminal:

import sys
sys.executable
>>> '/home/username/anaconda2/envs/tensorflow/bin/python' 

Also tensorflow gets imported with python shell

With ipython terminal inside conda environment, it shows same executable path. and tensorflow gets imported inside ipython shell.

However with jupyter notebook when I execute a cell in notebook, tensorflow module cannot be found. Also terminal spawned from notebook shows executable path of global python installation which is in anaconda/bin directoty, not of environment I had created from which I started the notebook

'/home/username/anaconda2/bin/python'

However conda environment of shell is still tensorflow

conda info --envs
# conda environments:                                                                                                              
#                                                                                                                                  
tensorflow            *  /home/username/anaconda2/envs/tensorflow                                                                     
root                     /home/username/anaconda2

Does that mean kernel is linked to python installation in this location and not in conda env? How to link the same?

There is some more nuance to this question that is good to clarify. Each notebook is bound to a particular kernel. With the latest 4.0 release of Anaconda we (Continuum) have bundled a Conda-environment-aware extension that will try to associate a Notebook with a particular Conda environment. If that cannot be found then the "default" environment (or "root" environment) will be used. In your case you have a Notebook that is, I am guessing, asking for the default (or "root") environment, and so Jupyter starts a kernel in that environment, and not in the environment from which the Jupyter server was started. You can change the associated kernel by going to the Kernel->Change kernel menu and picking your tensorflow environment's kernel, along the lines of this:

在此处输入图片说明

Or when you create a new Notebook you can pick at that time which Conda environment's kernel should back the Notebook (note that one Conda environment can have multiple kernels available, eg Python and R):

在此处输入图片说明

We appreciate that this can be a common cause of confusion, especially when sharing notebooks, since the person who shared it either used the "default" kernel (probably called just "Python"), or they were using a Conda environment with a different name. We are working on ways to make this smoother and less confusing, but if you have suggestions for expected/desired behavior, please let us know (GitHub issue to https://github.com/ContinuumIO/anaconda-issues/issues/new is the best way to do this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM