使用aptitud安装包时python的多个版本

2024-06-01 08:00:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在一台实验室机器上,我不能只去敲东西,似乎有一个以上的版本的python安装。

如果我是python——我看到的版本是2.7.1。

我已经通过“apt-get install numpy”安装了numpy,它说它已经安装了,但是当我尝试导入它时却找不到。

当我在机器上查找numpy时,可以在/usr/lib/python2.5/site-packages/numpy文件夹中看到它。我想这就是问题所在。。。把它放在2.5版本而不是2.7。

我该如何解决?有没有办法告诉apt在安装时我在说哪个python?或者我放弃天赋而使用pip什么的?


Tags: installpip版本numpy文件夹机器getlib
3条回答

Apt/dpkg有一种Debian方式来管理Python的多个已安装版本(我相信这叫做Python支持)。您安装的任何额外包(如numpy)都将自动生成,并可用于该包支持并由dpkg安装的所有Python版本。由于numpy支持每种Python,因此您的信息告诉我,系统中惟一的Debian Python包是2.5,路径中的2.7可能在/usr/local中。安装numpy包时,它不知道本地构建的2.7。您总是可以轻松安装。

使用virtualenv的建议很好。我有一个使用Python2.5支持的生产系统,它已经从debian unstable中删除;virtualenv使您可以使用任何需要的版本。由于很多工具都需要python,所以最好让系统python留在Debian希望的位置。

安装python模块的一个好的、标准的和简单的方法是pip

使用此命令,您可以安装一个带有命令的包(在终端中,而不是在python shell中)

pip install <packagename>

(具有根权限)

它负责处理依赖关系。

处理多个版本的python:

我不知道这是不是一个标准做法,但我这样做。

要在say version 2.5上安装软件包

python2.5 /usr/bin/pip install <packagename>

默认情况下,Ubuntu有多个python版本可用(例如2.4、2.6、2.7、3.2等)

在您的例子中,如果您不希望在python2.7上安装numpy(您应该IMO:) 你可以使用python2.5,你可以通过启动python2.5来使用numpy


    $python2.5

    >>> import numpy
    >>>

编辑:

如果您使用apt-get install,包将安装在系统默认的python版本上。

如果要在一台计算机上使用多个版本的python,应该研究virtualenv

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform's standard location is), it's easy to end up in a situation where you unintentionally upgrade an application that shouldn't be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).

这里有一个问题with a similar solution

此外,我使用virtualenvwrapper是因为我发现它使管理多个环境变得更加容易。

相关问题 更多 >