When Python imports but IPython does not
This happens when the sys.path
s of Python and IPython are different. To check the paths, enter the code below in each shell.
In the Python shell:
>>> import sys, pprint
>>> pprint.pprint(sys.path)
['',
'/Library/Python/2.7/site-packages',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']
>>>
In the IPython shell:
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/local/bin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages',
'/Library/Python/2.7/site-packages/IPython/extensions',
'/Users/username/.ipython']
In [3]:
In most cases, there will be a missing path from IPython’s sys.path
. In this case, that path is /usr/local/lib/python2.7/site-packages
. Now let’s try adding this path to IPython’s sys.path
.
Temporary solution
If a path exists in Python but is missing in IPython, you can temporarily add the corresponding path to IPython using sys.path.append
. For example, if /usr/local/lib/python2.7/site-packages
was missing from your IPython path, you can append it as below in the IPython shell:
In [1]: import sys
In [2]: sys.path.append('/usr/local/lib/python2.7/site-packages')
Permanant solution
Enter the following in the terminal.
ipython profile create
will create a new IPython profile, andipython locate
will tell you where your IPython profile is located. Of course, your IPython profile path will probably be different with the example below.$ ipython profile create $ ipython locate /Users/username/.ipython
Move to the path of your IPython profile printed above, and open the file
profile_default/ipython_config.py
.$ cd /Users/username/.ipython $ vi profile_default/ipython_config.py
Add the following to
profile_default/ipython_config.py
.c.InteractiveShellApp.exec_lines = [ 'import sys; sys.path.append("/usr/local/lib/python2.7/site-packages")' ]