Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook, wrong sys.path and sys.executable

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?

like image 285
Glenn Raskovich Avatar asked Oct 08 '17 18:10

Glenn Raskovich


People also ask

How do I change the path in Jupyter Notebook?

Change Jupyter Notebook startup folder (Windows) Copy the Jupyter Notebook launcher from the menu to the desktop. Right click on the new launcher and change the Target field , change %USERPROFILE% to the full path of the folder which will contain all the notebooks.

What is the default path for Jupyter Notebook?

Configuration files Config files are stored by default in the ~/. jupyter directory.

How do I change the file path in Jupyter lab?

In the start menu, right click Jupyter Notebook -> Properties . In the Target field, change %USERPROFILE% to your new "D:\path" .


2 Answers

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

like image 120
Glenn Raskovich Avatar answered Sep 21 '22 12:09

Glenn Raskovich


For me, I installed Jupyter after creating an environment, but then was trying to run a module installed from the base env. I found by "jupyter kernelspec list" (https://github.com/jupyter/notebook/issues/2563), my kernel.json at C:\Users\username\Anaconda37\share\jupyter\kernels\python3\kernel.json was pointing to the python.exe in my working env. Changed the path and solved it.

This was an exhaustive description of python path setting.

like image 32
alchemy Avatar answered Sep 18 '22 12:09

alchemy