问题
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']
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?
Note1: This question is about sys.path in python. Please don't recommend me to use virtualenv or change PYTHONPATH.
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
回答1:
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'
来源:https://stackoverflow.com/questions/42076014/python-modifying-sys-path-doesnt-work