Ipython 的 sys.path 与 python 不同,并且不使用 PYTHONPATH?

Posted

技术标签:

【中文标题】Ipython 的 sys.path 与 python 不同,并且不使用 PYTHONPATH?【英文标题】:Ipython has different sys.path than python and does not use PYTHONPATH? 【发布时间】:2012-08-25 15:50:11 【问题描述】:

IPython 似乎没有考虑我的 PYTHONPATH,而普通的 python 解释器会这样做。我在 Windows 7 上。

我的 Python 路径: C:\工作区\python; C:\Python27\Lib\site-packages\spyderlib; C:\Workspace\Python\awesim\awesim

打印系统路径:

import sys
for i in sorted(sys.path):
    print i

这是我在 IPython 中获得的:

C:\JModelica.org-1.8\Python C:\Python27 C:\Python27\DLLs C:\Python27\lib C:\Python27\lib\lib-tk C:\Python27\lib\plat-win C:\Python27\lib\site-packages C:\Python27\lib\site-packages\PIL C:\Python27\lib\site-packages\Pythonwin C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg\IPython\extensions C:\Python27\lib\site-packages\numpy-1.6.2-py2.7-win32.egg C:\Python27\lib\site-packages\openpyxl-1.5.8-py2.7.egg C:\Python27\lib\site-packages\pandas-0.8.1-py2.7-win32.egg C:\Python27\lib\site-packages\pyzmq-2.2.0.1-py2.7-win32.egg C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info C:\Python27\lib\site-packages\sphinx-1.1.3-py2.7.egg C:\Python27\lib\site-packages\statsmodels-0.4.0-py2.7-win32.egg C:\Python27\lib\site-packages\tornado-2.3-py2.7.egg C:\Python27\lib\site-packages\win32 C:\Python27\lib\site-packages\win32\lib C:\Python27\lib\site-packages\wx-2.8-msw-unicode C:\Python27\脚本 C:\windows\system32\python27.zip

在 python 控制台中也是如此:

C:\Python27 C:\Python27\DLLs C:\Python27\Lib\site-packages\spyderlib C:\Python27\lib C:\Python27\lib\lib-tk C:\Python27\lib\plat-win C:\Python27\lib\site-packages C:\Python27\lib\site-packages\PIL C:\Python27\lib\site-packages\Pythonwin C:\Python27\lib\site-packages\ipython-0.13-py2.7.egg C:\Python27\lib\site-packages\numpy-1.6.2-py2.7-win32.egg C:\Python27\lib\site-packages\openpyxl-1.5.8-py2.7.egg C:\Python27\lib\site-packages\pandas-0.8.1-py2.7-win32.egg C:\Python27\lib\site-packages\pyzmq-2.2.0.1-py2.7-win32.egg C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info C:\Python27\lib\site-packages\sphinx-1.1.3-py2.7.egg C:\Python27\lib\site-packages\statsmodels-0.4.0-py2.7-win32.egg C:\Python27\lib\site-packages\tornado-2.3-py2.7.egg C:\Python27\lib\site-packages\win32 C:\Python27\lib\site-packages\win32\lib C:\Python27\lib\site-packages\wx-2.8-msw-unicode C:\Workspace\Python\awesim\awesim C:\windows\system32\python27.zip C:\workspace\python

您可以看到正常的 python 控制台反映了 PYTHONPATH,但 IPython 输出没有。

提前感谢您的线索。

【问题讨论】:

该死,布局很抱歉,但由于某种原因,我似乎无法添加换行符。我希望问题是这样清楚的。 你是如何开始 IPython 的?在 IPython 中 os.environ['PYTHONPATH'] 得到什么? 我得到 'C:\\JModelica.org-1.8\\Python' @minrk :你找到了线索。当我通过在命令窗口中键入“ipython”启动 IPython 时,sys.path 是相同的。我从 windows/start 窗口中的快捷方式启动了 IPython...但我仍然不明白发生了什么。 如何设置 PYTHONPATH? setuptools 构建开始菜单项,因此它可能不会以继承您的环境的方式启动它们。 IPython 实际上并不控制这些东西。 我通过 Windows 控制面板/环境变量设置 PYTHONPATH。我尝试了我的用户配置文件和系统的设置。 【参考方案1】:

显然,当 Python 和 IPython 的 sys.paths 不同时会发生这种情况。

对于 IPython,一些快速的临时解决方案是:

import sys
sys.path.append('your paths')

我个人喜欢把它放在我正在处理的脚本中,以便包含在项目目录中组织的模块,包括它们的子目录。 (PS。不要​​忘记:如果主目录和所需的子目录包含(空)__init__.py 文件,python 会将子目录包含在路径中。)

永久的解决方案是创建一个新的 IPython 配置文件:

ipython profile create
ipython locate
/Users/username/.ipython

转到 ipython 配置文件并编辑: profile_default/ipython_config.py

添加以下内容

c.InteractiveShellApp.exec_lines = [
     'import sys; sys.path.append("you paths")'
 ]

这适用于 Linux,我猜应该也可以在 Windows 上运行。

【讨论】:

【参考方案2】:

从“开始”菜单链接的 .exe 启动器是由 setuptools 制作的,它们可能无法正确设置您的环境(我对 Windows 环境的了解不够,无法确定,或者是否可以修复) .

但是如果你从命令行启动 IPython,它肯定会继承你的环境。

【讨论】:

【参考方案3】:

我刚刚在运行 Python 2.6 的 Linux 上解决了一个类似的问题。

原来我的虚拟环境设置为忽略系统路径。

通过关闭所有python程序并运行来修复它:

virtualenv --system-site-packages ~

【讨论】:

以上是关于Ipython 的 sys.path 与 python 不同,并且不使用 PYTHONPATH?的主要内容,如果未能解决你的问题,请参考以下文章

python系统学习:模块积累(持续更新)

Python:系统变量路径已更改,与 sys.path 中的值不匹配

sys模块

sys模块

什么设置 sys.path 与 Python,什么时候?

python之常用模块学习