问题:如何在Windows上运行多个Python版本

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

如何指定我要使用哪个?

我正在使用Windows XP SP2。

I had two versions of Python installed on my machine (versions 2.6 and 2.5). I want to run 2.6 for one project and 2.5 for another.

How can I specify which I want to use?

I am working on Windows XP SP2.


回答 0

运行不同的Python副本就像启动正确的可执行文件一样容易。您提到您已经从命令行通过简单输入以下内容启动了python实例:python

这在Windows下的作用是拖曳%PATH%环境变量,检查可执行文件,无论是批处理文件(.bat),命令文件(.cmd)还是其他要运行的可执行文件(由可执行文件控制PATHEXT环境变量)是否与给定名称匹配。当找到正确的文件来运行时,该文件正在运行。

现在,如果您已经安装了两个Python版本2.5和2.6,则路径中将同时包含它们的两个目录,例如 PATH=c:\python\2.5;c:\python\2.6但是Windows将在找到匹配项时停止检查该路径。

您真正需要做的是显式调用一个或两个应用程序,例如c:\python\2.5\python.exec:\python\2.6\python.exe

另一种选择是创建一个快捷方式,以分别python.exe调用其中一个python25和另一个python26;然后python25,您只需在命令行上运行即可。

Running a different copy of Python is as easy as starting the correct executable. You mention that you’ve started a python instance, from the command line, by simply typing python.

What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.

Now, if you’ve installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.

What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.

The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.


回答 1

为该问题添加了两个解决方案:

  • 使用pylauncher(如果您使用的是Python 3.3或更高版本,则无需安装它,因为它已经随Python一起提供了),然后在脚本中添加shebang行;

#! c:\[path to Python 2.5]\python.exe-适用于要与Python 2.5一起运行
#! c:\[path to Python 2.6]\python.exe的脚本-适用于要与Python 2.6一起运行的脚本

或者代替运行python命令run pylauncher command(py)指定要使用哪个版本的Python;或者

py -2.6–版本2.6
py -2–最新安装的版本2.x
py -3.4–版本3.4
py -3–最新安装的版本3.x

virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]

virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]

例如

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

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

那么您可以激活第一个并像这样使用Python 2.5,
c:\venvs\2.5\activate
并且当您想切换到Python 2.6时,

deactivate  
c:\venvs\2.6\activate

Adding two more solutions to the problem:

  • Use pylauncher (if you have Python 3.3 or newer there’s no need to install it as it comes with Python already) and either add shebang lines to your scripts;

#! c:\[path to Python 2.5]\python.exe – for scripts you want to be run with Python 2.5
#! c:\[path to Python 2.6]\python.exe – for scripts you want to be run with Python 2.6

or instead of running python command run pylauncher command (py) specyfing which version of Python you want;

py -2.6 – version 2.6
py -2 – latest installed version 2.x
py -3.4 – version 3.4
py -3 – latest installed version 3.x

virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]

virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]

for example

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

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

then you can activate the first and work with Python 2.5 like this
c:\venvs\2.5\activate
and when you want to switch to Python 2.6 you do

deactivate  
c:\venvs\2.6\activate

回答 2

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

总结:py script.pyPython的版本中规定发布#!,如果或Python 2 #!缺失。该py -3 script.py运行Python 3。

From Python 3.3 on, there is the official Python launcher for Windows (http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonX to determine the wanted version of the interpreter also on Windows. See more details in my another comment or read the PEP 397.

Summary: The py script.py launches the Python version stated in #! or Python 2 if #! is missing. The py -3 script.py launches the Python 3.


回答 3

按照@alexander,您可以建立如下的符号链接集。将它们放在您的路径中包含的某个位置,以便可以轻松调用它们

> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe

只要您将c:\ bin或您放置在其中的任何位置都在路径中,现在就可以

> python25

As per @alexander you can make a set of symbolic links like below. Put them somewhere which is included in your path so they can be easily invoked

> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe

As long as c:\bin or where ever you placed them in is in your path you can now go

> python25

回答 4

  1. 安装python

    • C:\ Python27
    • C:\ Python36
  2. 环境变量

    • PYTHON2_HOME: C:\Python27
    • PYTHON3_HOME: C:\Python36
    • Path: %PYTHON2_HOME%;%PYTHON2_HOME%\Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%\Scripts;
  3. 文件重命名

    • C:\ Python27 \ python.exe→C:\ Python27 \ python2.exe
    • C:\ Python36 \ python.exe→C:\ Python36 \ python3.exe
  4. 点子

    • python2 -m pip install package
    • python3 -m pip install package
  1. install python

    • C:\Python27
    • C:\Python36
  2. environment variable

    • PYTHON2_HOME: C:\Python27
    • PYTHON3_HOME: C:\Python36
    • Path: %PYTHON2_HOME%;%PYTHON2_HOME%\Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%\Scripts;
  3. file rename

    • C:\Python27\python.exe → C:\Python27\python2.exe
    • C:\Python36\python.exe → C:\Python36\python3.exe
  4. pip

    • python2 -m pip install package
    • python3 -m pip install package

回答 5

例如对于3.6版本类型py -3.6。如果您同时具有32位和64位版本,则只需键入py -3.6-64或即可py -3.6-32

For example for 3.6 version type py -3.6. If you have also 32bit and 64bit versions, you can just type py -3.6-64 or py -3.6-32.


回答 6

当您安装Python时,它不会覆盖其他主要版本的其他安装。因此,安装Python 2.5.x不会覆盖Python 2.6.x,尽管安装2.6.6会覆盖2.6.5。

因此,您只需安装它即可。然后,调用所需的Python版本。例如:

C:\Python2.5\Python.exe

适用于Windows上的Python 2.5和

C:\Python2.6\Python.exe

适用于Windows上的Python 2.6,或

/usr/local/bin/python-2.5

要么

/usr/local/bin/python-2.6

Windows Unix(包括Linux和OS X)上。

在Unix(包括Linux和OS X)上python安装时,将安装通用命令,这是最后安装的命令。大多数情况下这不是问题,因为大多数脚本会显式调用/usr/local/bin/python2.5或一些用于防止这种情况的文件。但是,如果您不想这样做,并且您可能不想这样做,可以这样安装:

./configure
make
sudo make altinstall

请注意,“ altinstall”表示它将安装它,但不会代替python命令。

python据我所知,在Windows上您没有得到全局命令,所以这不是问题。

When you install Python, it will not overwrite other installs of other major versions. So installing Python 2.5.x will not overwrite Python 2.6.x, although installing 2.6.6 will overwrite 2.6.5.

So you can just install it. Then you call the Python version you want. For example:

C:\Python2.5\Python.exe

for Python 2.5 on windows and

C:\Python2.6\Python.exe

for Python 2.6 on windows, or

/usr/local/bin/python-2.5

or

/usr/local/bin/python-2.6

on Windows Unix (including Linux and OS X).

When you install on Unix (including Linux and OS X) you will get a generic python command installed, which will be the last one you installed. This is mostly not a problem as most scripts will explicitly call /usr/local/bin/python2.5 or something just to protect against that. But if you don’t want to do that, and you probably don’t you can install it like this:

./configure
make
sudo make altinstall

Note the “altinstall” that means it will install it, but it will not replace the python command.

On Windows you don’t get a global python command as far as I know so that’s not an issue.


回答 7

我强烈推荐pyenv-win项目。

在此处输入图片说明

由于kirankotari的工作,现在我们有了Windows版本的pyenv。

I strongly recommend the pyenv-win project.

enter image description here

Thanks to kirankotari‘s work, now we have a Windows version of pyenv.


回答 8

这是一个快速的技巧:

  1. 转到您要运行的python版本的目录
  2. 右键点击python.exe
  3. 选择“ 创建快捷方式
  4. 给该快捷方式起个呼叫的名字(我使用p27,p33等)
  5. 将该快捷方式移至您的主目录(C:\Users\Your name
  6. 打开命令提示符并输入name_of_your_shortcut.lnk(我使用p27.lnk

Here’s a quick hack:

  1. Go to the directory of the version of python you want to run
  2. Right click on python.exe
  3. Select ‘Create Shortcut
  4. Give that shortcut a name to call by( I use p27, p33 etc.)
  5. Move that shortcut to your home directory(C:\Users\Your name)
  6. Open a command prompt and enter name_of_your_shortcut.lnk(I use p27.lnk)

回答 9

cp c:\ python27 \ bin \ python.exe作为python2.7.exe

cp c:\ python34 \ bin \ python.exe作为python3.4.exe

它们都在系统路径中,请选择要运行的版本

C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

cp c:\python27\bin\python.exe as python2.7.exe

cp c:\python34\bin\python.exe as python3.4.exe

they are all in the system path, choose the version you want to run

C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

回答 10

使用批处理文件进行切换,在Windows 7上轻松高效。我使用以下命令:

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

在用户变量部分

  1. 在路径环境变量中添加了%pathpython%

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

在系统变量部分

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

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

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

文件内容=

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

     setx PathPython %PathPython%

要在版本之间切换,我在admin模式下执行批处理文件。

!!!!! 该更改对SUBSEQUENT命令提示符窗口OPENED有效。!!!

因此,我对此有完全的控制权。

Using a batch file to switch, easy and efficient on windows 7. I use this:

In the environment variable dialog (C:\Windows\System32\SystemPropertiesAdvanced.exe),

In the section user variables

  1. added %pathpython% to the path environment variable

  2. removed any references to python pathes

In the section system variables

  1. removed any references to python pathes

I created batch files for every python installation (exmple for 3.4 x64

Name = SetPathPython34x64 !!! ToExecuteAsAdmin.bat ;-) just to remember.

Content of the file =

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

     setx PathPython %PathPython%

To switch between versions, I execute the batch file in admin mode.

!!!!! The changes are effective for the SUBSEQUENT command prompt windows OPENED. !!!

So I have exact control on it.


回答 11

在Windows上运行多个版本的python的最简单方法如下所述:

1)从python.org/downloads下载最新版本的python通过选择系统的相关版本,。

2)运行安装程序,然后选择将python 3.x添加到路径中以在python 3中自动设置路径(您只需单击复选框)。对于python 2,请打开python 2安装程序,选择所需的任何首选项,但只需记住将Add python.exe设置为路径将其安装在本地硬盘上,现在只需单击下一步,然后等待安装程序完成即可。

3)两个安装都完成后。右键单击我的计算机-转到属性-选择高级系统设置-转到环境变量-单击系统变量下的新建,然后添加一个新的系统变量,其变量名称PY_PYTHON并将此变量值设置为3。现在单击确定,您应该完成。

4)现在要对此进行测试,请打开命令提示符。一旦进入pythonpy,它应该打开python3

5)现在通过键入exit()退出 python3 。现在输入py -2应该会打开python 2。

如果这些都不起作用,请重新启动计算机,如果问题仍然存在,请卸载所有组件并重复步骤。

谢谢。

The easiest way to run multiple versions of python on windows is described below as follows:-

1)Download the latest versions of python from python.org/downloads by selecting the relevant version for your system.

2)Run the installer and select Add python 3.x to the path to set path automatically in python 3 (you just have to click the checkbox). For python 2 open up your python 2 installer, select whatever preferences you want but just remember to set Add python.exe to path to Will be installed on local hard drive, Now just click next and wait for the installer to finish.

3)When both the installations are complete. Right click on my computer–Go to properties–Select advanced system settings–Go to environment variables–Click on new under System variables and add a new system variable with variable name as PY_PYTHON and set this variable value to 3. Now click on OK and you should be done.

4)Now to test this open the command prompt. Once you are in there type python or py, It should open up python3.

5)Now exit out of python3 by typing exit(). Now type py -2 it should open python 2.

If none of this works then restart the computer and if the problem still persists then uninstall everything and repeat the steps.

Thanks.


回答 12

您可以从Anaconda Navigator图形化地创建不同的python开发环境。在使用不同的python版本时遇到相同的问题,因此我使用anaconda导航器创建了不同的python开发环境,并在每个环境中使用了不同的python版本。

这是帮助文档。

https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/

You can create different python development environments graphically from Anaconda Navigator. I had same problem while working with different python versions so I used anaconda navigator to create different python development environments and used different python versions in each environments.

Here is the help documentation for this.

https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/


回答 13

使用Rapid Environment Editor, 您可以将所需的Python安装目录推到顶部。例如,要从c:\ Python27目录启动python,请确保c:\ Python27目录在Path环境变量中的c:\ Python36目录之前或之上。根据我的经验,正在执行Path环境中找到的第一个python可执行文件。例如,我已经在Python27上安装了MSYS2,并且由于我已经将C:\ MSYS2添加到C:\ Python36之前的路径中,因此正在执行C:\ MSYS2 ….文件夹中的python.exe。

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.


回答 14

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

Just call the correct executable


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。