Python:从多个已安装的模块版本中选择一个 [英] Python: select one of multiple installed module versions

查看:358
本文介绍了Python:从多个已安装的模块版本中选择一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的系统上,我多次安装了几个模块.举个例子,在标准路径中的/usr/lib/python2.7/dist-packages处安装了numpy 1.6.1,我在/local/python/lib/python2.7/site-packages/处安装了numpy 1.8.0的更新版本.

On my system, I have several modules installed multiple times. To give an example, numpy 1.6.1 is installed in the standard path at /usr/lib/python2.7/dist-packages, and I have an updated version of numpy 1.8.0 installed at /local/python/lib/python2.7/site-packages/.

我不能简单地删除旧版本的原因是我无权更改工作计算机上的任何内容.但是,我需要使用新的numpy版本.

The reason I cannot simply remove the old version is that I do not have permissions to change anything on my work computer. I however need to use the new numpy version.

我已将/local/python/lib/python2.7/site-packages/添加到我的PYTHONPATH中.不幸的是,这没有帮助,因为首先将/usr/lib/python2.7/dist-packages插入路径,因此将加载numpy 1.6.1.这是一个示例:

I have added /local/python/lib/python2.7/site-packages/ to my PYTHONPATH. Unfortunately, this does not help, since /usr/lib/python2.7/dist-packages is inserted into the path first and therefore, numpy 1.6.1 will be loaded. Here's an example:

>>> import os
>>> print os.environ['PYTHONPATH']
/local/python/lib/python2.7/site-packages
>>> import pprint
>>> import sys
>>> pprint.pprint(sys.path)
['',
 '/local/python/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg',
 '/local/python/lib/python2.7/site-packages/pyparsing-2.0.1-py2.7.egg',
 '~/.local/lib/python2.7/site-packages/setuptools-3.4.4-py2.7.egg',
 '~/.local/lib/python2.7/site-packages/mpldatacursor-0.5_dev-py2.7.egg',
 '/usr/lib/python2.7/dist-packages',
 '/local/python/lib/python2.7/site-packages',
 '/usr/lib/python2.7',
 ...,
 '~/.local/lib/python2.7/dist-packages', 
 ...]

所以,导入顺序似乎是

  1. 当前目录
  2. PYTHONPATH 中的蛋
  3. 来自本地模块路径(~/.local/lib/python2.7/site-packages/*.egg)的鸡蛋
  4. 系统范围的模块路径(~/usr/lib/python2.7/dist-packages/)
  5. PYTHONPATH 中的目录
  6. 中间路径(为简便起见,省略)
  7. 用户库目录(~/.local/lib/python2.7/site-packages/)
  1. current directory
  2. eggs from PYTHONPATH
  3. eggs from local module path (~/.local/lib/python2.7/site-packages/*.egg)
  4. system-wide module path (~/usr/lib/python2.7/dist-packages/)
  5. directories from PYTHONPATH
  6. intermediate paths (omitted for brevity)
  7. userbase directory (~/.local/lib/python2.7/site-packages/)

我的问题是我需要将第5项放在第3项和第4项之前,以便我的代码正常工作.现在,如果我从/local/*目录导入针对numpy 1.8.0编译的模块,并且此模块导入numpy,它将仍然从/usr/*目录获取numpy并失败.

My problem is that I would need to put item 5. before items 3. and 4. for my code to work properly. Right now, if I import a module that was compiled against numpy 1.8.0 from the /local/* directory, and this module imports numpy, it will still take numpy from the /usr/* directory and fail.

我通过在脚本中放置以下内容来解决此问题:

I have circumvented this problem by placing something like this in my scripts:

import sys
sys.path.insert(0, '/local/python/lib/python2.7/site-packages/')

因此,我可以强制Python使用正确的导入顺序,但这当然不是解决方案,因为我必须在每个单个脚本中执行此操作.

Thereby I can force Python to use the right import order, but of course this is not a solution, since I would have to do this in every single script.

推荐答案

除了注释部分已经给出的建议外,您是否考虑过使用

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.

例如,您可以像这样纯粹地安装和设置它(virtualenv-1.11.6 外观是当前的最新版本):

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

上面,我们创建了一个名为"numpyvenv"的虚拟环境,激活了该环境,安装了numpy,打印了numpy版本(以显示其工作原理),退出了python,并停用了该环境.下次激活环境时,numpy以及安装的所有其他模块都将在其中.尝试此操作时,您可能会打h,但这应该可以帮助您开始.

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.

这篇关于Python:从多个已安装的模块版本中选择一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆