我在 /opt/lib/python2.7/site-packages 下有一个新的 numpy 版本,在 /usr/lib/python2.7/下有一个标准(系统)版本dist 包.我想暂时使用新的 numpy 版本,所以我在脚本的开头添加了以下内容:
I have a new numpy version under /opt/lib/python2.7/site-packages, and a standard (system) version under /usr/lib/python2.7/dist-packages. I want to temporarily use the new numpy version so I add the following at the beginning of my script:
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 版本:
Strangely it still loads the old numpy version:
In [4]: import numpy as np In [5]: np.__file__ Out[5]: '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'有人对此有任何想法吗?
Does anyone has any idea about this?
注1:这个问题是关于python中的sys.path.请不要推荐我使用 virtualenv 或更改 PYTHONPATH.
Note1: This question is about sys.path in python. Please don't recommend me to use virtualenv or change PYTHONPATH.
注2:在我新建的numpy目录下找到以下文件
Note2: the following files are found under my new numpy directory
$ 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 推荐答案显然我必须将 numpy .egg 文件添加到 sys.path 中才能使其工作.
Apparently I have to add the numpy .egg file to sys.path in order to make it work.
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'如果我只添加站点包,它不起作用:
It doesn't work if I add the site-packages only:
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'更多推荐
python修改sys.path不起作用
发布评论