9

似乎每个人都推荐 virtualenv 用于多个 python 版本(在 osx 上),但它甚至可以与 python 3.0 一起使用吗?我下载了它,但它似乎没有......而且我真的不明白它是如何工作的,你可以一次“打开”环境吗?我想要的是离开系统 python 2.5(显然),并让 python 3.1.1 和 subversion pygame 来编写我自己的东西,以及 python 2.6 和正常的稳定 pygame 用来运行其他东西,比如从 pygame 下载的 pygame 游戏.org。关于如何实现这一点的任何帮助?谢谢。

好的,我意识到 virtualenv 不是我想要的。

4

5 回答 5

14

It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.

As I understand it you want to create different virtual environments with different Python versions?

This is very easy, and you only need virtualenv itself.

For, say, a Python 3:

$ virtualenv -p python3 p34env

(...)
New python executable in p34env/bin/python3.4
Also creating executable in p34env/bin/python
Installing setuptools, pip...done.

$ source p34env/bin/activate

(p34env)$ python -V
Python 3.4.2

(p34env)$ deactivate 
$

You use the source command to activate the venv, and deactivate to - you guessed it - deactivate it. Notice the prompt changes to indicate the env.

For your system's standard version of Python you just skip the -p python3 argument, and you can use the argument to point to any version you want given a path.

The last argument is the name (p34env) and you can make as many as you like, just give them different names.

于 2015-02-03T12:19:05.607 回答
3

您的用例实际上并不需要 virtualenv。你只需要安装几个不同的 Python 版本。

于 2009-09-12T21:18:14.770 回答
1

对我来说virtualenv3效果很好。我还安装了 pypi.python.org/pypi/distribute。这也适用于提到的 www.doughellmann.com/docs/virtualenvwrapper/ 。不过,我只在 Linux 上测试过。

于 2010-07-09T09:39:29.140 回答
1

virtualenv旨在创建 Python 环境的隔离环境。将它与多个 Python 实例一起使用的技巧是安装virtualenv到您想要使用它的每个 Python 版本中,例如:

/usr/bin/easy_install-2.6 virtualenv
/usr/local/bin/easy_install virtualenv
sudo port install py26-virtualenv

或使用预期的 Python 版本调用它,例如:

/usr/bin/python2.6 virtualenv.py ENV
/usr/local/bin/python2.6 virtualenv.py ENV
/opt/local/bin/python2.5 virtualenv.py ENV

因此,因此,它并不能直接解决您想要使用哪个 Python 的问题(在 OS X 上尤其严重)。有多种方法可以解决这个问题:使用预期 Python 的绝对路径(如上面的示例)、定义 shell 别名、仔细管理$PATH搜索顺序等。

目前,virtualenvPython 3 不支持 AFAIK,因为除其他外, Python 3 尚不支持setuptools(easy_install 背后的魔力),尽管针对该解决方案的工作正在进行中。

顺便说一句,许多人使用 Doug Hellman 的virtualenvwrapper来简化 virtualenv 的使用。

于 2009-09-12T21:12:19.930 回答
0

不确定我是否理解正确,但这里是:)

我不知道 OS X,但是在 Linux 中你可以同时安装 2.6 和 3。然后你可以指定使用 python25 或 python3,或者将 /usr/bin/python 符号链接更改为默认情况下要使用的版本.

于 2009-09-12T20:25:12.113 回答