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

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

我正在使用Windows XP SP2。


当前回答

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版本并希望使用pipenv创建虚拟环境的其他人有所帮助。

进入工程目录,执行py -[python版本]PIP install pipenv,例如:py -3.6 PIP install pipenv 运行pipenv——python [version]以创建所需python版本的虚拟环境。示例:pipenv——python 3.6 运行pipenv shell激活虚拟环境。

这里有一个解决方案:

首先,在你的电脑上安装你想要运行的所有版本。https://www.python.org/ 其次,创建您想使用的python版本的虚拟环境。 "py [python_version] -m venv [vrtual_environment_name]"示例:"py -3.9 -m venv env"

注意:你不需要运行"pip install virtualenv"

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

使用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.

在windows上运行多个版本的python的最简单方法如下

1)从python.org/downloads下载最新版本的python,选择适合您系统的版本。

2)运行安装程序,选择“Add python 3”。X到python 3中自动设置路径的路径(你只需要单击复选框)。对于python2,打开你的python2安装程序,选择任何你想要的首选项,但要记住将添加python.exe到路径设置为将被安装在本地硬盘驱动器上,现在只需单击下一步,等待安装程序完成。

3)两个安装完成后。右键单击我的计算机—转到属性—选择高级系统设置—转到环境变量—单击系统变量下的new,添加一个新的系统变量,变量名为PY_PYTHON,并将该变量值设置为3。现在点击OK,你应该完成了。

4)现在要测试这个,打开命令提示符。一旦你在那里输入python或py,它应该打开python3。

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

如果这些都不行,那么重新启动计算机,如果问题仍然存在,那么卸载所有软件并重复上述步骤。

谢谢。