我对编程比较陌生,并且一直搜索到我的指尖是蓝色的,但似乎无法找到解决我遇到的问题的方法。
我在 OSX 上安装了 Python 2 和 Python 3 的自制版本,我似乎无法在我的 .bash_profile 中获得正确的 PATH/PYTHONPATH 以便能够在 IDLE 的两个版本中正确导入模块。但是,当直接在 shell 窗口中运行 Python 2 或 Python 3 时,我可以导入模块。我正在通过终端启动 IDLE,因此它应该正确初始化路径。
这是我的.bash_profile
:
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:$PYTHONPATH
以下是所有四种情况下发生的情况,使用 numpy 作为示例模块。
在终端运行 python2 时导入有效:
$ python2
Python 2.7.5 (default, Jun 28 2013, 19:06:25)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
从终端启动 Python 2 的 IDLE,我也可以导入 numpy. 但是,当我sys.path
在 IDLE 和sys.path
终端中进行比较时,它们是不同的。我比较了列表以产生差异并发现:
>>> InIdleNotInTerm
['/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/bin']
>>> InTermNotInIdle
[]
因此,在运行 Python 2 时,IDLE 路径中多了一个目录。
Python 3 的情况完全不同。
$ python3
Python 3.3.2 (default, Jul 1 2013, 10:53:26)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
但是,如果我为 Python 3 启动 IDLE(通过在终端中输入 idle3)然后尝试导入 numpy,我会得到
>>> import numpy as np
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
>>>
同样,我sys.path
在终端和 IDLE 中进行了比较,这次有很大的不同:
>>> pp(InIdleNotInTerm)
['/Library/Frameworks/Python.framework/Versions/3.3/bin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages']
>>> pp(InTermNotInIdle)
['/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info',
'/usr/local/lib/python3.3/site-packages',
'/usr/local/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info']
知道发生了什么吗?我尝试过的其他一些事情:
将 .bash_profile 更改为
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:/usr/local/lib/python3.3/site-packages/:$PYTHONPATH
导致能够在终端和 IDLE 中导入 Python 3 中的模块,但会中断 Python 2 的导入(因为它会尝试导入 Python 3 版本!)
通过终端为 Python 3 启动 IDLEpython3.3/site-packages/
可以正确导入所有内容,但我不想每次启动 Python 3 时都这样做。
您的任何想法或想法将不胜感激!谢谢 :)