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

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

问题描述

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

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

我已将 /local/python/lib/python2.7/site-packages/ 添加到我的 PYTHONPATH.不幸的是,这无济于事,因为 /usr/lib/python2.7/dist-packages 首先插入到路径中,因此将加载 numpy 1.6.1.举个例子:

<预><代码>>>>导入操作系统>>>打印 os.environ['PYTHONPATH']/local/python/lib/python2.7/site-packages>>>导入打印>>>导入系统>>>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/)

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

我通过在我的脚本中放置这样的东西来规避这个问题:

导入系统sys.path.insert(0, '/local/python/lib/python2.7/site-packages/')

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

解决方案

除了评论区已经给出的建议,你有没有想过使用 virtualenv?这将使您可以对要使用的每个模块进行细粒度控制.如果您不熟悉 virtualenv,则需要阅读文档以了解其工作原理.

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

$ 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$ 源 ./bin/activate(numpyvenv) $ pip install numpy# 下载、编译并将 numpy 安装到虚拟环境中(numpyvenv) $ python输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入 numpy>>>numpy.version.version'1.9.1'>>>退出()(numpyvenv) $ 停用$ # 虚拟环境已停用

上面,我们创建了一个名为numpyvenv"的虚拟环境,激活了环境,安装了numpy,打印了numpy的版本(以表明它有效),退出python,并停用了环境.下次激活环境时,numpy 将与您安装的任何其他模块一起出现.尝试此操作时您可能会遇到问题,但它应该可以帮助您入门.

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/.

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.

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', 
 ...]

So, it seems that the import order is

  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/)

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/')

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.

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.

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

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