我的机器上安装了两个版本的Python(版本2.6和2.5)。我想在一个项目上运行2.6,在另一个项目上运行2.5。

我如何指定我想要使用哪个?

我正在使用Windows XP SP2。


当前回答

一个简单的方法是使用

Py -3.8 -m PIP安装virtualenv在这里-3.8与你的[版本号]

安装virtualenv之后,可以使用

Py -3.8 -m virtualenv[你的环境名称]

然后CD到venv,输入activate

这将激活您喜欢的python版本。 只需更改版本号以使用不同的python版本。

其他回答

只需调用正确的可执行文件

为这个问题增加了两个解决方案:

使用pylauncher(如果你有Python 3.3或更新版本,不需要安装它,因为Python已经自带了),或者在你的脚本中添加shebang行;

# !c:\[Python 2.5的路径]\ Python .exe -用于您希望在Python 2.5中运行的脚本 # !c:\[Python 2.6的路径]\ Python .exe -用于您希望在Python 2.6中运行的脚本

或者不是运行python命令,而是运行pylauncher命令(py) specyfing你想要的python版本;

Py -2.6 -版本2.6 Py -2 -最新安装版本2.x Py -3.4 -版本3.4 Py -3 -最新安装版本3.x

安装virtualenv并创建两个virtualenv;

virtualenv -p c:\[Python 2.5的路径]\ Python .exe[使用Python 2.5创建virtualenv的路径]\[virtualenv的名称]

virtualenv -p c:\[Python 2.6的路径]\ Python .exe[使用Python 2.6创建virtualenv的路径]\[virtualenv的名称]

例如

Virtualenv -p c:\python2.5\python.exe c:\venvs\2.5 .exe

Virtualenv -p c:\python2.6\python.exe c:\venvs\2.6 .exe

然后你可以激活第一个并像这样使用Python 2.5 c: \ venvs \ 2.5 \激活 当你想切换到Python 2.6时,你可以这样做

deactivate  
c:\venvs\2.6\activate

Using the Rapid Environment Editor you can push to the top the directory of the desired Python installation. For example, to start python from the c:\Python27 directory, ensure that c:\Python27 directory is before or on top of the c:\Python36 directory in the Path environment variable. From my experience, the first python executable found in the Path environment is being executed. For example, I have MSYS2 installed with Python27 and since I've added C:\MSYS2 to the path before C:\Python36, the python.exe from the C:\MSYS2.... folder is being executed.

从Python 3.3开始,Windows有官方的Python启动器(http://www.python.org/dev/peps/pep-0397/)。现在,您可以使用#!pythonX来确定在Windows上需要的解释器版本。在我的另一条评论中查看更多细节,或阅读PEP 397。

概要:py script.py会启动#!如果#!不见了。py -3 script.py启动Python 3。

使用批处理文件切换,在windows 7上简单高效。我用这个:

在环境变量对话框(C:\Windows\System32\SystemPropertiesAdvanced.exe)

在部分用户变量

添加了%pathpython%到path环境变量 删除了对python路径的任何引用

在部分系统变量

删除了对python路径的任何引用

我为每个python安装都创建了批处理文件(例如3.4 x64)

Name = SetPathPython34x64 !!ToExecuteAsAdmin.bat;-)只是为了记住。

文件的内容=

     Set PathPython=C:\Python36AMD64\Scripts\;C:\Python36AMD64\;C:\Tcl\bin

     setx PathPython %PathPython%

为了在版本之间切换,我以管理模式执行批处理文件。

! !这些更改对随后的命令提示符窗口“已打开”有效。!!!

所以我可以完全控制它。