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

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

我正在使用Windows XP SP2。


当前回答

安装python C: \ Python27 C: \ Python36 环境变量 PYTHON2_HOME: C: \ Python27 PYTHON3_HOME: C: \ Python36 路径:% PYTHON2_HOME %, % PYTHON2_HOME % \脚本;% PYTHON3_HOME %, % PYTHON3_HOME % \脚本; 文件重命名 C:\Python27\python2.exe→C:\Python27\python2.exe C:\Python36\python3.exe→C:\Python36\python3.exe 皮普 Python2 -m PIP安装包 Python3 -m PIP安装包

其他回答

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

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

我认为这个答案可能对拥有多个python版本并希望使用pipenv创建虚拟环境的其他人有所帮助。

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

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

py -0

使用python版本来做某事

py -*version*

ex.

py -3.8 venv venv

将在python 3.8中创建虚拟环境

注意:

python -0 
 or
python -3.8

不工作,我认为它必须是“py”

根据@alexander,你可以像下面这样创建一组符号链接。把它们放在包含在路径中的某个地方,这样就可以很容易地调用它们

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

只要c:\bin或者你放置它们的地方在你的路径中,你现在就可以去了

> python25