安装 Python 3
Python 3.3 及更高版本将 py.exe 放入 windows 文件夹。 [link]
此可执行文件用于通过文件的第一行确定 python 版本:
#!/usr/bin/python2.7
将在 Python 2.7 中执行。
安装其他 Python 版本后,必须安装 Python 3 版本。
其他资源:https://docs.python.org/3/using/windows.html#customization
pywin https://pypi.python.org/pypi/pywin
旧解决方案
我猜你用的是窗户。我通过 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
或
#!/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
到
C:\python26
等等。与不使用 python 32 的 python 相同。
您可能还希望通过 klick
启动 python 文件
然后这样做:
在 .py 文件上单击鼠标右键
-> 打开
-> 选择 C:\bin\python.bat
如果您遇到问题,请联系我或发表评论。