安装Python 3
Python 3.3 及更高版本放置了py.exe
进入windows文件夹。 [link https://stackoverflow.com/questions/5087831/how-should-i-set-default-python-version]
该可执行文件用于通过文件的第一行确定 python 版本:
#!/usr/bin/python2.7
将使用Python 2.7执行。
安装其他 Python 版本后,必须安装 Python 3 版本。
其他资源:https://docs.python.org/3/using/windows.html#customization https://docs.python.org/3/using/windows.html#customization
pywin https://pypi.python.org/pypi/pywin https://pypi.python.org/pypi/pywin
旧解决方案
我猜你用的是windows。我用 hack 解决了这个问题:
每次我在 Windows 上启动 python 时,都会使用 python.bat 。
这会启动一个 python.py 来分析文件中的标头#!对于Python版本。
要启动 example.py,我在控制台中输入
python example.py
但也可以按以下顺序启动klick.
这是我的 python 文件
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 文件
@echo off
C:\bin\python2 C:\bin\python.py %*
rem this may also work:
rem C:\bin\python.py %*
在您启动的每个文件中,您必须放置
#!/bin/env/python3
or
#!/bin/env/python2
默认是python2
然后我将这些文件添加到该文件夹中:
C:\bin\python2.bat
@echo off
C:\python27\python.exe %*
C:\bin\pythonw2.bat
@echo off
C:\python27\pythonw.exe %*
C:\python3.bat
@echo off
C:\python32\pythonw.exe %*
C:\bin\pythonw3.bat
@echo off
C:\python32\pythonw.exe %*
如果您使用 python26 而不是 python27 那么您需要更改
C:\python27
to
C:\python26
等等。与不使用 python 32 的 python 相同。
您可能还想启动 python 文件klick
然后这样做:
右键单击 .py 文件
-> 打开方式
-> 选择 C:\bin\python.bat
如果您遇到问题,请联系我或发表评论。