min*_*nty 146 python windows compatibility python-3.x
我正在运行Windows,当您在命令行上运行程序时,shell/OS会根据注册表设置自动运行Python.如果我在同一台机器上安装2.x和3.x版本的Python,这会破坏吗?
我想玩Python 3,同时仍然可以在同一台机器上运行2.x脚本.
Nic*_*k T 65
共存的官方解决方案似乎是用于Windows的Python Launcher,PEP 397,它包含在Python 3.3.0中.将发布转储py.exe和pyw.exe启动器安装到%SYSTEMROOT%(C:\Windows)中,然后分别py与pyw脚本和脚本相关联.
要使用新的启动程序(无需手动设置自己的关联),请启用"注册扩展"选项.我不太清楚为什么,但在我的机器上它将Py 2.7作为"默认"(发射器).
通过直接从命令行调用脚本来运行脚本将路由它们通过启动程序并解析shebang(如果存在).您还可以显式调用启动器并使用开关:py -3 mypy2script.py.
各种各样的shebang似乎工作
#!C:\Python33\python.exe#!python3#!/usr/bin/env python3以及肆意滥用
#! notepad.exeAli*_*tin 39
这是我的设置:
C:\Python34(默认安装路径)并将python.exe更改为python3.exeC:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;现在在命令行中,您可以使用python2.7和python33.4.
Pat*_*ins 36
你可以安装两个.
你应该在脚本前面写这个:
#!/bin/env python2.7
Run Code Online (Sandbox Code Playgroud)
或者,最终......
#!/bin/env python3.6
Run Code Online (Sandbox Code Playgroud)
我的解决方案与Unix完美配合,在Google上快速搜索之后,这是Windows解决方案:
#!c:/Python/python3_6.exe -u
Run Code Online (Sandbox Code Playgroud)
同样的事情:在你的剧本面前.
Iva*_*rak 33
从版本3.3开始,Python引入了Launcher for Windows实用程序https://docs.python.org/3/using/windows.html#python-launcher-for-windows.
因此,为了能够使用多个版本的Python:
小智 9
我在shell中使用2.5,2.6和3.0,并使用以下形式的一行批处理脚本:
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
Run Code Online (Sandbox Code Playgroud)
pythonX.Y.bat将它们命名并将它们放在PATH中的某个位置.将首选次要版本(即最新版本)的文件复制到pythonX.bat.(例如copy python2.6.bat python2.bat)然后你可以python2 file.py在任何地方使用.
但是,这对Windows文件关联情况没有帮助甚至影响.为此,您需要一个读取该#!行的启动程序,然后将其与.py和.pyw文件相关联.
当您将两者都添加到环境变量时,会出现冲突,因为两个可执行文件具有相同的名称:python.exe.
只需重命名其中一个.在我的情况下,我将其重命名为python3.exe.
因此,当我运行python它将执行python.exe2.7时,当我运行python3它将执行python3.exe3.6
注意:应该使用python.exe脚本完成相同的操作.
小智 7
干得好...
winpylaunch.py
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
# #!/usr/bin/python
# #!C:\Python26\python.exe
# # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
# HKEY_CLASSES_ROOT\Python.File\shell\open\command
# (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#
import subprocess
import sys
USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""
if __name__ == "__main__":
if len(sys.argv) > 1:
script = sys.argv[1]
args = sys.argv[2:]
if script.endswith(".py"):
interpreter = "python.exe" # Default to wherever it is found on the path.
lines = open(script).readlines()
for line in lines:
if line.startswith("#!") and line.find(".exe") != -1:
interpreter = line[2:].strip()
break
process = subprocess.Popen([interpreter] + [script] + args)
process.wait()
sys.exit()
print(USAGE)
Run Code Online (Sandbox Code Playgroud)
我刚刚读完了这个帖子(因为这也是我需要的).我在Ubuntu和Windows上都有Pythons 2.6.1和3.0.1.如果它不适合您在这里发布修复程序.
尝试使用 Anaconda。
使用 Anaconda 环境的概念,假设您需要 Python 3 来学习编程,但您不想通过更新 Python 来消除您的 Python 2.7 环境。您可以创建并激活一个名为“snakes”(或任何您想要的)的新环境,并安装最新版本的 Python 3,如下所示:
conda create --name snakes python=3
Run Code Online (Sandbox Code Playgroud)
它比听起来更简单,请查看这里的介绍页面:Anaconda 入门
然后要处理让版本 2.x 和 3.x 并排运行的特定问题,请参阅:
这是在 Windows 上安装 Python2 和 Python3 的一种简洁明了的方法。
我的情况:我必须安装 Apache cassandra。我已经在D: 驱动器中安装了 Python3 。由于正在进行大量的开发工作,我不想弄乱我的 Python3 安装。而且,我只需要为 Apache cassandra 使用 Python2。
所以我采取了以下步骤:
C:\Python27;C:\Python27\Scripts)所以,我的 Python3 安装保持不变。
据我所知,Python 使用 PATH 变量而不是注册表设置从命令行运行。
因此,如果您在 PATH 上指向正确的版本,您将使用它。请记住重新启动命令提示符以使用新的 PATH 设置。