如何在不卸载的情况下使用多个版本的Python [英] How to use multiple versions of Python without uninstallation
问题描述
我面临着一种独特的情况,虽然微不足道,但却痛苦不堪.
I am faced with a unique situation, slightly trivial but painful.
我需要使用Python 2.6.6,因为NLTK并未移植到Python 3(这就是我可以收集到的东西).
I need to use Python 2.6.6 because NLTK is not ported to Python 3 (that's what I could gather).
在不同的代码(可同时运行)中,有一个collections计数器函数,仅在Python 3中可用,而在Python 2.6.6中不可用.
In a different code(which am working concurrently), there is a collections counter function which is available only in Python 3 but not in Python 2.6.6.
因此,每次我在两个代码之间切换时,都需要安装&卸载版本.那真是浪费时间.
So, each time I switch between the two codes, I need to install & uninstall the versions. That's such a waste of time.
关于如何指定要使用哪个版本的任何建议?
Any suggestions on how I specify which version I want to use?
推荐答案
安装Python 3
Python 3.3和更高版本将py.exe
放入Windows文件夹. [链接]
此可执行文件用于确定文件第一行的python版本:
Python 3.3 and higher put a py.exe
into the windows folder. [link]
This executable is used to determine the python version with the first line of the file:
#!/usr/bin/python2.7
将在Python 2.7中执行. 在安装其他Python版本之后,您必须安装Python 3版本.
will be executed with Python 2.7. You must install the Python 3 version after you installed the other Python versions.
其他资源: https://docs.python.org/3/使用/windows.html#customization
pywin https://pypi.python.org/pypi/pywin
旧解决方案
我猜您使用Windows.我用骇客解决了这个问题:
I guess you use windows. I solved this problem with a hack:
每次我在Windows上启动python时,都会使用python.bat. 这会启动一个python.py,该文件会分析文件的#!之后的标头以获取python版本.
Every time I start python on windows a python.bat will be used. This starts a python.py that analyses the file for the header after #! for the python version.
要启动example.py,请在控制台中输入
To start example.py I type into the console
python example.py
,但也可以按 kick 启动.
这是我的python文件 C:\ bin \ python.py
this is my python file C:\bin\python.py
#!/usr/bin/env python2
import sys
import os
args = sys.argv
if len(args) <= 1:
# no arguments
# start python console
i = os.system('C:\bin\python2.bat' + " ".join(args[1:]))
if type(i) != int:
i = 0
exit(i)
def analyse(filename, default = ''):
'''=> '2', '3', default '''
try:
f = open(filename)
except IOError:
# file not found
return default
firstLine = f.readline()
if firstLine.startswith('#!'):
if 'python2' in firstLine:
return '2'
if 'python3' in firstLine:
return '3'
i = firstLine.find(' ')
if i != -1:
# analyse from end of path on
in2 = '2' in firstLine[i:]
in3 = '3' in firstLine[i:]
if in2 and not in3:
return '2'
if in3 and not in2:
return '3'
else:
# analyse path
in2 = '2' in firstLine
in3 = '3' in firstLine
if in2 and not in3:
return '2'
if in3 and not in2:
return '3'
return default
no = analyse(args[1], default = '2')
if args[1][-1:] == 'w':
# python win
cmd = 'C:\bin\pythonw%s.bat'
else:
cmd = 'C:\bin\python%s.bat'
i = os.system(cmd % no + ' ' + " ".join(args[1:]))
if type(i) != int:
i = 0
exit(i)
这是C:\ bin \ python.bat文件
This is the C:\bin\python.bat file
@echo off
C:\bin\python2 C:\bin\python.py %*
rem this may also work:
rem C:\bin\python.py %*
在您启动的每个文件中,您都必须放入其中一个
and in every file you start you have to put either
#!/bin/env/python3
或
#!/bin/env/python2
默认为python2
default is python2
然后我将这些文件添加到该文件夹:
Then I added those files to the folder:
C:\ bin \ python2.bat
C:\bin\python2.bat
@echo off
C:\python27\python.exe %*
C:\ bin \ pythonw2.bat
C:\bin\pythonw2.bat
@echo off
C:\python27\pythonw.exe %*
C:\ python3.bat
C:\python3.bat
@echo off
C:\python32\pythonw.exe %*
C:\ bin \ pythonw3.bat
C:\bin\pythonw3.bat
@echo off
C:\python32\pythonw.exe %*
如果您使用的是python26而不是python27,那么您需要进行更改
If you are using python26 instead if python27 then you need to change
C:\python27
到
C:\python26
,依此类推.与不使用python 32的python相同.
and so on. Same with python not using python 32.
您可能还想按 klick
然后执行以下操作:
在.py文件上右击 ->打开 ->选择C:\ bin \ python.bat
klick right on a .py file -> open with -> select C:\bin\python.bat
如果遇到问题,请与我联系或发表评论.
If you get problems contact me or leave a comment.
这篇关于如何在不卸载的情况下使用多个版本的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!