如何从命令行在 python 2.7 和 python 3 之间切换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18058389/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:49:35  来源:igfitidea点击:

How to switch between python 2.7 to python 3 from command line?

pythonwindowscommand-linecmd

提问by JPC

I'm trying to find the best way to switch between the two python compilers, 2.7 to 3.3. I ran the python script from the cmd like this:

我试图找到在两个 python 编译器 2.7 到 3.3 之间切换的最佳方法。我像这样从 cmd 运行 python 脚本:

python ex1.py

python ex1.py

Where do I set the "python" environment in the window's environment variable to point to either python 3.3 or 2.7?

我在哪里设置窗口环境变量中的“python”环境以指向 python 3.3 或 2.7?

I am wondering if there is an easy way to switch between the two versions from the cmd line?

我想知道是否有一种简单的方法可以从 cmd 行在两个版本之间切换?

采纳答案by Sukrit Kalra

For Windows 7, I just rename the python.exefrom the Python 3 folder to python3.exeand add the path into the environment variables. Using that, I can execute python test_script.pyand the script runs with Python 2.7 and when I do python3 test_script.py, it runs the script in Python 3.

对于 Windows 7,我只是将python.exePython 3 文件夹中的重命名为python3.exe并将路径添加到环境变量中。使用它,我可以执行python test_script.py并且脚本使用 Python 2.7 运行,当我这样做时python3 test_script.py,它会在 Python 3 中运行脚本。

To add Python 3to the environment variables, follow these steps -

要添加Python 3到环境变量,请按照下列步骤操作 -

  1. Right Click on My Computer and go to Properties.
  2. Go to Advanced System Settings.
  3. Click on Environment Variablesand edit PATHand add the path to your Python 3 installation directory.
  1. 右键单击“我的电脑”并转到Properties
  2. Advanced System Settings
  3. 单击Environment Variables并编辑PATH并将路径添加到您的 Python 3 安装目录。

For example,

例如,

enter image description here

在此处输入图片说明

回答by Mark Tolonen

No need for "tricks". Python 3.3 comes with PyLauncher "py.exe", installs it in the path, and registers it as the ".py" extension handler. With it, a special comment at the top of a script tells the launcher which version of Python to run:

不需要“技巧”。Python 3.3 附带 PyLauncher“py.exe”,将其安装在路径中,并将其注册为“.py”扩展处理程序。有了它,脚本顶部的特殊注释会告诉启动器运行哪个版本的 Python:

#!python2
print "hello"

Or

或者

#!python3
print("hello")

From the command line:

从命令行:

Py -3 hello.py

Or

或者

Py -2 hello.py

See 3.4. Python Launcher for Windows

3.4。适用于 Windows 的 Python 启动器

回答by ahmadore

You can try to rename the python executable in the python3 folder to python3, that is if it was named python formally... it worked for me

您可以尝试将python3文件夹中的python可执行文件重命名为python3,即如果它正式命名为python...它对我有用

回答by Timothy Mugayi

They are 3 ways you can achieve this using the py command (py-launcher) in python 3, virtual environment or configuring your default python system path. For illustration purpose, you may see tutorial https://www.youtube.com/watch?v=ynDlb0n27cw&t=38s

它们是 3 种方法,您可以使用 python 3、虚拟环境中的 py 命令 (py-launcher) 或配置默认的 python 系统路径来实现此目的。出于说明目的,您可能会看到教程https://www.youtube.com/watch?v=ynDlb0n27cw&t=38s

回答by Philip L

In case you have both python 2 and 3 in your path, you can move up the Python27 folder in your path, so it search and executes python 2 first.

如果您的路径中有 python 2 和 3,您可以将路径中的 Python27 文件夹向上移动,以便它首先搜索并执行 python 2。

回答by ehambright

There is an easier way than all of the above; You can use the PY_PYTHON environment variable. From inside the cmd.exe shell;

有一种比以上所有方法更简单的方法;您可以使用 PY_PYTHON 环境变量。从 cmd.exe shell 内部;

For the latest version of Python 2

对于最新版本的 Python 2

set PY_PYTHON=2

For the latest version of Python 3

对于最新版本的 Python 3

set PY_PYTHON=3

If you want it to be permanent, set it in the control panel. Or use setx instead of set in the cmd.exe shell.

如果您希望它是永久性的,请在控制面板中进行设置。或者在 cmd.exe shell 中使用 setx 而不是 set 。