Python: select one of multiple installed module versions

百般思念 提交于 2019-11-27 19:22:15

Besides the suggestions already given in the comment section, have you thought about using virtualenv? This would give you fine-grained control over every module that you want to use. If you're not familiar with virtualenv you'll want to read the documentation to get a feel for how it works.

Purely for example, you could install and set it up, like so (virtualenv-1.11.6 looks to be the most recent version currently):

$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.11.6.tar.gz
$ tar xvfz virtualenv-1.11.6.tar.gz
$ cd virtualenv-1.11.6
$ python virtualenv.py ../numpyvenv
$ cd ../numpyvenv
$ source ./bin/activate
(numpyvenv) $ pip install numpy
# downloads, compiles, and installs numpy into the virtual environemnt
(numpyvenv) $ python
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.version.version
'1.9.1'
>>> quit()
(numpyvenv) $ deactivate
$ # the virtual environment has been deactivated

Above, we created a virtual environment named "numpyvenv", activated the environment, installed numpy, printed the numpy version (to show it works), quit python, and deactivated the environment. Next time you activate the environment, numpy will be there along with whatever other modules you install. You may run into hiccups while trying this, but it should get you started.

I had this problem on a Mac I was using without administrator access. My solution was the following:

  1. Find the directory of the numpy version you want to use. For me this was /Library/Python/2.7/site-packages

  2. Create a file ~/.startup.py and point to it with PYTHONSTARTUP=~/.startup.py in your .bashrc file

  3. In .startup.py:

import sys

sys.path.insert(0,'/Library/Python/2.7/site-packages/') <--- imports this BEFORE the standard parts

import numpy

print("Importing numpy version"+numpy.__version__) <---- To remind that we have changed the numpy version

This seems to work fine for me. I hope it helps.

While a virtualenv seems the way to go, as of Force python to use an older version of module (than what I have installed now) you can also use a modification of

import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted

I had the same issue on Debian Wheezy after installing the latest numpy module with easy_install.

The new numpy module was installed in /usr/local/lib/python2.7/dist-packages/numpy while the old module was in /usr/lib/pymodules/python2.7/numpy. When I tried to import the numpy module, the older version was imported. And as you say, adding to PYTHONPATH the new module path does not help, because is added in the sys.path below the older entry.

The issue seem to be in easy-install, because it creates a file easy-install.pth that imports /usr/lib/pymodules/python2.7 before any local module.

To fix the issue I just edited the file /usr/local/lib/python2.7/dist-packages/easy-install.pth and commented out the line /usr/lib/pymodules/python2.7 so this line will be placed below in the sys.path.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!