Python:选择多个已安装的模块版本之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23832722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:29:22  来源:igfitidea点击:

Python: select one of multiple installed module versions

pythonmodulepythonpathversionssys.path

提问by Jenny

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

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

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

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

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

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

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.0from the /local/*directory, and this module imports numpy, it will still take numpy from the /usr/*directory and fail.

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

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.

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

回答by rkersh

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?这将使您可以对要使用的每个模块进行细粒度控制。如果您不熟悉 virtualenv,您将需要阅读文档以了解它的工作原理。

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

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

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

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

回答by Pedro Piter

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

使用easy_install安装最新的numpy模块后,我在Debian Wheezy上遇到了同样的问题。

The new numpy module was installed in /usr/local/lib/python2.7/dist-packages/numpywhile 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 PYTHONPATHthe new module path does not help, because is added in the sys.pathbelow the older entry.

新的 numpy 模块安装在/usr/local/lib/python2.7/dist-packages/numpy旧模块中,而旧模块安装在/usr/lib/pymodules/python2.7/numpy. 当我尝试导入 numpy 模块时,导入了旧版本。正如您所说,添加到PYTHONPATH新模块路径无济于事,因为在sys.path下面的旧条目中添加了。

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

问题似乎出在easy-install,因为它创建了一个easy-install.pth在任何本地模块之前导入 /usr/lib/pymodules/python2.7的文件。

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

为了解决这个问题,我只是编辑了文件/usr/local/lib/python2.7/dist-packages/easy-install.pth并注释掉了该行,/usr/lib/pymodules/python2.7因此该行将被放置在 sys.path 的下方。

回答by Nick Walkden

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

我在没有管理员访问权限的情况下使用的 Mac 上遇到了这个问题。我的解决方案如下:

  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.pyand point to it with PYTHONSTARTUP=~/.startup.pyin your .bashrc file

  3. In .startup.py:

  1. 找到您要使用的 numpy 版本的目录。对我来说这是/Library/Python/2.7/site-packages

  2. 创建一个文件~/.startup.pyPYTHONSTARTUP=~/.startup.py在 .bashrc 文件中指向它

  3. .startup.py

import sys

import sys

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

sys.path.insert(0,'/Library/Python/2.7/site-packages/')<--- 在标准件之前导入

import numpy

import numpy

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

print("Importing numpy version"+numpy.__version__)<---- 提醒我们更改了numpy版本

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

这对我来说似乎很好。我希望它有帮助。

回答by serv-inc

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

虽然 virtualenv 似乎是要走的路,但从Force python 使用旧版本的模块(比我现在安装的版本)起,您还可以使用修改

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