我该如何在Python 2.7中更改sys.path路径的优先级?我知道可以使用PYTHONPATH
环境变量,但这是我得到的:
$ PYTHONPATH=/tmp python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in sys.path:
... print i
...
/usr/local/lib/python2.7/dist-packages/pycuda-2014.1-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/pytest-2.6.2-py2.7.egg
/usr/local/lib/python2.7/dist-packages/pytools-2014.3-py2.7.egg
/usr/local/lib/python2.7/dist-packages/py-1.4.24-py2.7.egg
/usr/lib/python2.7/dist-packages
/tmp
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
>>>
在/usr/lib/python2.7/dist-packages
和/usr/lib/python2.7
之间添加/tmp
。
我的目标是让Python首先从/usr/local/lib/python2.7/dist-packages
加载包。
这是我想要的:
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.version
<module 'numpy.version' from '/usr/local/lib/python2.7/dist-packages/numpy/version.pyc'>
>>>
如果我通过
apt-get install python-numpy
命令安装python-numpy
,Python会尝试从/usr/lib/python2.7
加载而不是我编译的版本。
/usr/lib
加载模块之前从/usr/local
加载模块,那么问题在哪里? - Bakuriuimport site
导致的。如果您使用-S
选项启动解释器,则会发现/tmp
被添加到列表开头而不是其他路径之间。 - Bakuriu