你如何在python 2和3之间切换,反之亦然?

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

How do you switch between python 2 and 3, and vice versa?

pythonpython-3.xpython-2.6

提问by rayne117

I am reading How To Learn Python The Hard Way, which uses 2. Recently discovered Invent With Python, which uses 3.

我正在阅读 How To Learn Python The Hard Way,它使用 2。最近发现 Invent With Python,它使用 3。

Can I download python 3, and use it when I read Invent With Python, then switch back to python 2 when I want to read How To Learn Python The Hard Way. If so, how would I choose which version I use?

我可以下载 python 3,并在阅读 Invent With Python 时使用它,然后在我想阅读 How To Learn Python The Hard Way 时切换回 python 2。如果是这样,我将如何选择我使用的版本?

回答by John

Yes you can. On my machine at least(Vista), v2 and v3 have completely separate idles allowing me to run whichever version I feel like when I feel like it. So go ahead and download v3.

是的你可以。至少在我的机器上(Vista),v2 和 v3 有完全独立的空闲,让我可以在我喜欢的时候运行我喜欢的任何版本。所以继续下载v3。

回答by GreenMatt

A couple ways on *nix systems:

*nix 系统上的几种方法:

  • Install into separate directories (e.g. /usr/local/python2 and /usr/local/python3) and create a link (e.g. /usr/bin/python) which you change to point to whichever executable you want to use.
  • Same install as above, but set up separate python commands (e.g. /usr/bin/python2 and /usr/bin/python3) and call those when you want to invoke python. Or have the python command default to one of those and a pythonN for the other (N = 2 or 3, whichever isn't the default).
  • 安装到单独的目录(例如 /usr/local/python2 和 /usr/local/python3)并创建一个链接(例如 /usr/bin/python),您可以将其更改为指向您想要使用的任何可执行文件。
  • 与上面相同的安装,但设置单独的 python 命令(例如 /usr/bin/python2 和 /usr/bin/python3)并在你想调用 python 时调用它们。或者让 python 命令默认为其中之一,而 pythonN 为另一个(N = 2 或 3,以不是默认值为准)。

回答by Corey Goldberg

depends on your system/platform...

取决于您的系统/平台...

I'm currently on Ubuntu 10.10 and have both 2.6 and 3.1 installed. The default system python is 2.6, and python3 is installed as an additional package.

我目前在 Ubuntu 10.10 上安装了 2.6 和 3.1。默认系统python是2.6,python3作为附加包安装。

corey@studio17:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
corey@studio17:~$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

similarly, on Windows, I have 2.6 and 3.1 installed (in C:\Python26 and C:\Python31)

同样,在 Windows 上,我安装了 2.6 和 3.1(在 C:\Python26 和 C:\Python31 中)

easy to switch back and forth.

容易来回切换。



also.. there are some syntactic differences between 2.x and 3.x that you will need to be aware of (print is a function, etc).

此外.. 2.x 和 3.x 之间存在一些语法差异,您需要注意(打印是一个函数等)。

回答by Vijay Shetty

Using 'virtualenv'you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.

使用“virtualenv”,您可以在一台机器上拥有不同的独立 Python 环境。您也可以随时在不同的 Python 解释器版本之间切换。

What is virtualenv?

什么是虚拟环境?

A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn't actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.

虚拟环境是 Python 的独立工作副本,它允许您在特定项目上工作而不必担心影响其他项目。它支持 Python 的多个并行安装,每个项目一个。它实际上并没有安装单独的 Python 副本,但它确实提供了一种保持不同项目环境隔离的巧妙方法。

How to install?

如何安装?

pip install virtualenv

To create virtual environment for python 2.7 :

为 python 2.7 创建虚拟环境:

root:~# which python2.7

/usr/bin/python2.7


root:~# which python3.4

/usr/local/bin/python3.4

You can also use a Python interpreter of your choice:

您还可以使用您选择的 Python 解释器:

root:~# virtualenv -p /usr/bin/python2.7 Vpy27

Running virtualenv with interpreter /usr/bin/python2.7

New python executable in /root/Vpy27/bin/python2.7

Also creating executable in /root/Vpy27/bin/python

Installing setuptools, pip, wheel...done.

To begin using the virtual environment, it needs to be activated:

要开始使用虚拟环境,需要激活它:

root:~# source Vpy27/bin/activate

The name of the current virtual environment will now appear on the left of the prompt:

当前虚拟环境的名称现在将出现在提示的左侧:

(Vpy27) root:~# python -V
Python 2.7.3

Install packages as usual, for example:

像往常一样安装软件包,例如:

(Vpy27) root:~# pip install junos-eznc    >> All pip installs done here, will be available only in this environment.

If you are done working in the virtual environment for the moment, you can deactivate it:

如果您目前已在虚拟环境中完成工作,您可以停用它:

(Vpy27) root:~# deactivate   

To create virtual environment for python 3.4:

为 python 3.4 创建虚拟环境:

root:~# which python3.4

/usr/local/bin/python3.4

root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34

root:~# source Vpy34/bin/activate

(Vpy34) root:~# python -V
Python 3.4.4

There is also a way to create virtual environment with already available site-packages.

还有一种方法可以使用已经可用的站点包创建虚拟环境。

回答by SeasonalShot

Here are my 2 cents.

这是我的 2 美分。

If you are on a unix based system (Ubuntu, etc..), and you currently have python 2.x. Go ahead and download the python 3.x from Python.org

如果您使用的是基于 Unix 的系统(Ubuntu 等),并且您目前拥有 python 2.x。继续从Python.org下载 python 3.x

After installation it will create a separate directory python3

安装后会创建一个单独的目录 python3

You are done.

你完成了。

To run your programs with python2.xuse python filename.py

运行你的程序python2.x使用python filename.py

To run your programs with python3.x, use python3 filename.py

要使用 运行您的程序python3.x,请使用python3 filename.py

Similarly, to fire up the python2.xand python 3.xinterpreter use pythonand python3respectively.

同样,要分别使用and来启动python2.xpython 3.x解释器。pythonpython3

I see some of the answers pointing you to virtualenv, I don't think that is what you were asking for, virtualenv is used for isolating Python environments.

我看到一些答案将您指向 virtualenv,我认为这不是您要的,virtualenv 用于隔离 Python 环境。

回答by Elijah W. Gagne

I'm on Windows 10 with Python 3.5 and 2.7. Using PowerShell, here's how I'm switching between versions.

我在 Windows 10 上使用 Python 3.5 和 2.7。使用 PowerShell,这是我在版本之间切换的方式。

############################################################
# Switch to Python 2.7
############################################################

# Remove python 3.5 from PATH
$current_path = [Environment]::GetEnvironmentVariable("Path", "User")
$python3_path = "C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\;"
$new_path = $current_path.replace($python3_path, "")
[Environment]::SetEnvironmentVariable("Path", $new_path, "User")

# Add python 2.7 to PATH
# Run PowerShell as administrator
$current_path = [Environment]::GetEnvironmentVariable("Path", "Machine")
$python2_path = "C:\Python27\;C:\Python27\Scripts;"
$new_path = $python2_path + $current_path
[Environment]::SetEnvironmentVariable("Path", $new_path, "Machine")

# Restart PowerShell to see change

# Confirm change
python --version


############################################################
# Switch to Python 3.5
############################################################

# Remove python 2.7 from PATH
# Run PowerShell as administrator
$current_path = [Environment]::GetEnvironmentVariable("Path", "Machine")
$python2_path = "C:\Python27\;C:\Python27\Scripts;"
$new_path = $current_path.replace($python2_path, "")
[Environment]::SetEnvironmentVariable("Path", $new_path, "Machine")

# Add python 3.5 to PATH
$current_path = [Environment]::GetEnvironmentVariable("Path", "User")
$python3_path = "C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\;"
$new_path = $python3_path + $current_path
[Environment]::SetEnvironmentVariable("Path", $new_path, "User")

# Restart PowerShell to see change

# Confirm change
python --version

Note that you will need to update paths to reflect your Python versions and user profile.

请注意,您需要更新路径以反映您的 Python 版本和用户配置文件。

回答by xmcp

On Windows, the Python launchercan do this for you.

在 Windows 上,Python 启动器可以为您执行此操作。

The PEP article says:

PEP 文章说:

Shebang line parsing

If the first command-line argument does not start with a dash ('-') character, an attempt will be made to open that argument as a file and parsed for a shebang line according to the rules in [1]::

#! interpreter [optional-arg]

舍邦线解析

如果第一个命令行参数不以破折号 ('-') 字符开头,则会尝试将该参数作为文件打开并根据 [1] 中的规则解析为 shebang 行:

#! interpreter [optional-arg]

So simply add a shebang at the beginning of your Python script, like this:

因此,只需在 Python 脚本的开头添加一个 shebang,如下所示:

#! python3
#coding=utf-8

import sys
print(sys.version)
...

Or you can pass a command-line parameter to the py.exelauncher:

或者您可以将命令行参数传递给py.exe启动器:

C:\Users\Administrator>py -3 my_script.py

C:\Users\Administrator>py -2 my_script.py

回答by Ahmed Teir

if you are using windows 10 and have python 2.x and 3.x.

如果您使用的是 Windows 10 并且有 python 2.x 和 3.x。

  1. open control panel > system and security > system
  2. click advanced system settings.
  3. click environment variables.
  4. click path and edit and then make the path of python version you want to use above that you don't want to use [by click the moveu Up button]
  5. restart powershell.
  6. python --version
  1. 打开控制面板>系统和安全>系统
  2. 单击高级系统设置。
  3. 单击环境变量。
  4. 单击路径并进行编辑,然后将要使用的python 版本的路径设置为不想使用的路径[通过单击 moveu Up 按钮]
  5. 重启powershell。
  6. 蟒蛇--版本

回答by user3065757

In windows 10 it is a lot easier then what have been given by users above.

在 Windows 10 中,它比上面用户提供的要容易得多。

Install both the version in separate folders, and then go to environment variable and add the path for both the versions.

将两个版本安装在单独的文件夹中,然后转到环境变量并添加两个版本的路径。

Now any time you want to run particular version, just change its order (path) and move it to top of other version, and then restart the cmd and type python this time, you will see that only that particular version of python will run.

现在任何时候你想运行特定版本,只需改变它的顺序(路径)并将其移动到其他版本的顶部,然后重新启动cmd并这次键入python,您将看到只有该特定版本的python会运行。

How to switch between python 2 and 3

如何在python 2和3之间切换

For example in my case, I have two version of python one in anaconda(v3.0.6) and another is python 2.7

例如,在我的情况下,我有两个版本的 python,一个是 anaconda(v3.0.6),另一个是 python 2.7

anytime I want to run the 2.7 i move its path above the anaconda version, as you can see in the screenshot above, and move it below when i want to run anaconda version.

任何时候我想运行 2.7 时,我都会将其路径移到 anaconda 版本上方,如您在上面的屏幕截图中所见,并在我想运行 anaconda 版本时将其移到下方。

回答by jturi

I've tried 6 solutions so far, like:

到目前为止,我已经尝试了 6 种解决方案,例如:

virtualenv --python=python py27env
mkvirtualenv --python=python3 py3env etc..

also using virtualenvwrapper package etc. None of them worked.

还使用 virtualenvwrapper 包等。他们都没有工作。

I have Python 3.6 and Python2.7 on my Windows 10 machine, so I renamed C:\Python27\python.exe to python2.exe and C:\Python36\python.exe to python3.exe or you can even use python36.exe format. Of course C:\Python27, C:\Python27\Scripts, C:\Python36, C:\Python36\Scripts added to Environment Variables PATH.

我的 Windows 10 机器上有 Python 3.6 和 Python2.7,所以我将 C:\Python27\python.exe 重命名为 python2.exe,将 C:\Python36\python.exe 重命名为 python3.exe,或者你甚至可以使用 python36.exe格式。当然 C:\Python27, C:\Python27\Scripts, C:\Python36, C:\Python36\Scripts 添加到环境变量 PATH 中。

(1) For python3 just type:

(1) 对于 python3 只需键入:

python3 -m virtualenv venv3

python3 -m virtualenv venv3

(2) Go to venv folder, activate it with:

(2) 进入 venv 文件夹,使用以下命令激活它:

Scripts\activate.bat

脚本\激活.bat

(3) (venv3) shows it's activated:

(3) (venv3) 显示它已激活:

(venv3) HOME1@PC C:\Builts\venv3

(4) and then check if it is really 3.6:

(4)然后检查是否真的是3.6:

python --version

蟒蛇--版本

Python 3.6.0


For python2:

对于python2:

python2 -m virtualenv venv2

python2 -m virtualenv venv2

Result:

结果:

(venv2) HOME1@PC C:\Builts\venv2
python --version
Python 2.7.9

I hope it will help.

我希望它会有所帮助。