Jupyter notebook, wrong sys.path and sys.executable

Glenn Raskovich picture Glenn Raskovich · Oct 8, 2017 · Viewed 11.5k times · Source

I'm trying to run the anaconda distribution of python libraries in a Jupyter Notebook, but when I run the notebook I keep getting ImportErrors because the python path is set to the default distribution from Mac OS X 10.11

When I print out the sys.path and sys.executable, they differ when running python vs running jupyter notebook. For example,

from pprint import pprint as p
import sys

p(sys.path)

After doing this in python I get the correct output:

['',
 '/Users/glennraskovich/anaconda2/lib/python27.zip',
 '/Users/glennraskovich/anaconda2/lib/python2.7',
 '/Users/glennraskovich/anaconda2/lib/python2.7/plat-darwin',
 '/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac',
 '/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/glennraskovich/anaconda2/lib/python2.7/lib-tk',
 '/Users/glennraskovich/anaconda2/lib/python2.7/lib-old',
 '/Users/glennraskovich/anaconda2/lib/python2.7/lib-dynload',
 '/Users/glennraskovich/anaconda2/lib/python2.7/site-packages',
 '/Users/glennraskovich/anaconda2/lib/python2.7/site-packages/aeosa']

But when running this in jupyter notebook I get:

['',
 '/usr/local/lib/python2.7/site-packages/dask-0.11.0-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/networkx-1.11-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/Pillow-3.3.1-py2.7-macosx-10.11-x86_64.egg',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages',
 '/Library/Python/2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/IPython/extensions',
 '/Users/glennraskovich/.ipython']

For the sys.executable,

p(sys.executable)

In python, correct output:

/Users/glennraskovich/anaconda2/bin/python

But in jupyter notebook, sys.executable is not set to the anaconda version

/usr/local/opt/python/bin/python2.7

I've tried setting PATH in my .bashrc and .bash_profile, and using the commands which python, which jupyter and such show anaconda paths but jupyter notebook is not using the anaconda paths. What could be the issue here?

Answer

Glenn Raskovich picture Glenn Raskovich · Oct 9, 2017

I figured out the solution, since the kernel was set to use the default mac os x's python I fixed it by using the commands

python2 -m pip install ipykernel

python2 -m ipykernel install --user