When Python imports but IPython does not

本文内容转载自博客

This happens when the sys.paths 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, and ipython 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")'
 ]

本文内容转载自博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值