Python和IPython路径不同

时间:2014-03-27 23:08:45

标签: python bash ipython .bash-profile .profile

我正在一台我没有管理员权限的服务器上工作,因此我已经在本地安装了软件包pip --user。为了让python看到这条路径,我将其添加到我的.profile:

export PYTHONPATH=$PYTHONPATH:/Users/delavega/Library/Python/2.7/lib/python/site-packages

这可以成功地将本地安装的软件包添加到python的路径中,这可以通过调用sys.path()来证明。但是,IPython 使用本地安装中的软件包,而是使用全局安装的软件包:/usr/local/lib/python2.7/site-packages

打印python和IPython的路径,我的本地站点包文件夹位于不同的位置但路径总体上有所不同:

的Python:

['',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.8-x86_64.egg',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-2.2-py2.7.egg',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.8-x86_64.egg',
 '/usr/local/lib/python2.7/site-packages/setuptools-2.2-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/Users/delavega',
 '/Users/delavega/Library/Python/2.7/lib/python/site-packages',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 '/Library/Python/2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages']

IPython的:

['',
 '/usr/local/lib/python2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.8-x86_64.egg',
 '/usr/local/lib/python2.7/site-packages/setuptools-2.2-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/bin',
 '/usr/local/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/Users/delavega',
 '/Users/delavega/Library/Python/2.7/lib/python/site-packages',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Python/2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/IPython/extensions']

如您所见,路径不同。使用sys追加或预先添加IPython的路径不会改变加载的包

为什么这些路径不同,如何从本地安装加载IPython?

1 个答案:

答案 0 :(得分:0)

确保ipython是virtualenv-aware的最可靠方法是将pip安装到你的virtualenv中。

肯定有ipython_config.py hacks(http://rodesia.org/2012/09/04/making-ipython-virtualenv-aware/)试图给它一些意识,但YMMV。