修改sys.path的python不起作用

时间:2017-02-06 19:45:43

标签: python-2.7 numpy path

我在/opt/lib/python2.7/site-packages下有一个新的numpy版本,在/usr/lib/python2.7/dist-packages下有一个标准(系统)版本。我想暂时使用新的numpy版本,所以我在脚本的开头添加了以下内容:

In [1]: import sys

In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages')

In [3]: sys.path
Out[3]: 
['',
 '/opt/numpy/lib/python2.7/site-packages',
 '/usr/local/bin',
 '/usr/lib/python2.7/dist-packages',
 '/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/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/lib/python2.7/dist-packages/IPython/extensions']

奇怪的是,它仍然会加载旧的numpy版本:

In [4]: import numpy as np

In [5]: np.__file__
Out[5]: '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'

有没有人对此有任何想法?

注1:这个问题是关于python中的sys.path。请不要建议我使用virtualenv或更改PYTHONPATH。

注意2:在我的新numpy目录下找到以下文件

$ ls /opt/numpy/lib/python2.7/site-packages
easy-install.pth  numpy-1.11.0-py2.7-linux-x86_64.egg  __pycache__  site.py  site.pyc

1 个答案:

答案 0 :(得分:0)

显然我必须将numpy .egg 文件添加到sys.path才能使其正常工作。

In [1]: import sys

In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg')

In [3]: sys.path
Out[3]: 
['',
 '/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg',
 '/usr/local/bin',
 '/usr/lib/python2.7/dist-packages',
 '/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/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/lib/python2.7/dist-packages/IPython/extensions']

In [4]: import numpy as np

In [5]: np.__file__
Out[5]: '/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg/numpy/__init__.pyc'

如果我只添加网站包,它不起作用:

In [1]: import sys

In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages')

In [3]: sys.path
Out[3]: 
['',
 '/opt/numpy/lib/python2.7/site-packages',
 '/usr/local/bin',
 '/usr/lib/python2.7/dist-packages',
 '/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/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/lib/python2.7/dist-packages/IPython/extensions']

In [4]: import numpy as np

In [5]: np.__file__
Out[5]: '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'