如何在python2和python3中运行程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4940617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run programs in python2 and python3
提问by kachilous
I have python 2.6.6 and python 3.1.3 currently installed on my machine (Windows Vista 64 bit) My path variable includes the directory of both versions. How can I specify which python I want to run a program in. For instance, if I want to run a program in python 3, it works but if I want to run a different program in python2 I get a syntax error. So how can I run a python 2 program in the cmd?
我的机器上当前安装了 python 2.6.6 和 python 3.1.3(Windows Vista 64 位)我的路径变量包括两个版本的目录。如何指定我想在哪个 python 中运行程序。例如,如果我想在 python 3 中运行一个程序,它可以工作,但如果我想在 python2 中运行另一个程序,我会收到语法错误。那么如何在 cmd 中运行 python 2 程序呢?
Typing python in my command line, python 3.1.3 is the only one that shows up.
在我的命令行中输入 python,python 3.1.3 是唯一出现的。
采纳答案by Michael Aaron Safyan
You can specify the version in the executable name python2.6and python3.
您可以在可执行文件名称python2.6和python3.
回答by duffymo
Instead of just typing "python" on the command line, use the full path the python.exe that you want to run: FULL_PATH_TO_PYTHON_2.6.6\python.exe or FULL_PATH_TO_PYTHON_3.1.3\python.exe should distinguish between the two.
不要只在命令行上输入“python”,而是使用要运行的 python.exe 的完整路径:FULL_PATH_TO_PYTHON_2.6.6\python.exe 或 FULL_PATH_TO_PYTHON_3.1.3\python.exe 应该区分两者。
回答by atx
The shell will read the PATH from left to right, so you most likely defined Python 3.1.3 before Python 2.6.6. Specify the full path for each to use both versions.
shell 将从左到右读取 PATH,因此您很可能在 Python 2.6.6 之前定义了 Python 3.1.3。为每个版本指定完整路径以使用两个版本。
回答by Ayman
The Python Launcheris probably what you need. I used it with 2.7 and 3.2.
在Python的启动可能是你所需要的。我在 2.7 和 3.2 中使用了它。
回答by Alberto Perez
回答by imankalyan
You can also use: 'py -main_version script_name.py args'
您还可以使用:'py -main_version script_name.py args'
Example:
例子:
py -2 script_name.py args for Python 2.X
py -2 script_name.py args for Python 2.X
py -3 script_name.py args for Python 3.X
py -3 script_name.py args for Python 3.X
To test both are working or not, you can try,
要测试两者是否有效,您可以尝试,
>> py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>> py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.


