Home > Backend Development > Python Tutorial > How to switch python2 and python3

How to switch python2 and python3

爱喝马黛茶的安东尼
Release: 2019-06-19 16:36:38
Original
6583 people have browsed it

How to switch python2 and python3

How to switch between python2 and python3? The switching methods are introduced below in the windows environment and Linux environment:

windows environment:

Install in the windows environment How to switch between python2 and python3? Use

to enter py -3 to enter python3

Enter py -2 to enter python2

Linux Environment:

#Why we need two versions of Python

Every developer who has come into contact with Python knows that Python2 and Python3 are incompatible Although Python3 is the future, there are still many projects developed using Python2. Many distributions of Linux (such as Ubuntu) come with Python 2.7, but when we are ready to develop a Python 3 project, what should we do?

Then download Python3 too. Well, it is indeed possible to install the two together under Linux, but the question is how do you switch between the two versions of Python.

1 Modify the alias

First let’s take a look at our default Python version

$ python --versionPython 2.7.6
Copy after login

Then we modify the alias

$ alias python='/usr/bin/python3'$ python --versionPython 3.4.3  # 版本已经改变
Copy after login

/usr/bin/python3 How was this path found?

Generally speaking, software binary files can be found in /usr/bin or /usr/local/bin (this one has a higher priority). Of course, if you are using Debian Linux, you can find it like this (provided you have installed Python3):

$ dpkg -L python3

The above alias modification is only temporary. , the configuration will disappear after reopening a window. If you want each window to use this alias, you can edit ~/.bashrc (if you are using another shell, it is not this file, such as zsh is ~/.zshrc) and write the alias configuration into the file.

The advantage of modifying the alias is that it is simple enough, but the switch is inflexible.

Related recommendations: "Python Video Tutorial"

2 Link file

Create a link in /usr/bin The file points to Python3.

$ ln -s python /usr/bin/python3$ python --versionPython 3.4.3
Copy after login

Just like modifying the alias, the modification is not flexible enough.

3 Use update-alternatives to switch versions

update-alternatives is a tool provided by Debian (you don’t need to read it if you are not a Debian system). The principle is similar to the one above The method is also through links, but the switching process is very convenient.

First take a look at the help information of update-alternatives:

$ update-alternatives --help
用法:update-alternatives [<选项> ...] <命令>
 
命令:
  --install <链接> <名称> <路径> <优先级>
    [--slave <链接> <名称> <路径>] ...
                           在系统中加入一组候选项。
  --remove <名称> <路径>   从 <名称> 替换组中去除 <路径> 项。
  --remove-all <名称>      从替换系统中删除 <名称> 替换组。
  --auto <名称>            将 <名称> 的主链接切换到自动模式。
  --display <名称>         显示关于 <名称> 替换组的信息。
  --query <名称>           机器可读版的 --display <名称>.
  --list <名称>            列出 <名称> 替换组中所有的可用候选项。
  --get-selections         列出主要候选项名称以及它们的状态。
  --set-selections         从标准输入中读入候选项的状态。
  --config <名称>          列出 <名称> 替换组中的可选项,并就使用其中
                           哪一个,征询用户的意见。
  --set <名称> <路径>      将 <路径> 设置为 <名称> 的候选项。
  --all                    对所有可选项一一调用 --config 命令。
 
<链接> 是指向 /etc/alternatives/<名称> 的符号链接。
    (如 /usr/bin/pager)
<名称> 是该链接替换组的主控名。
    (如 pager)
<路径> 是候选项目标文件的位置。
    (如 /usr/bin/less)
<优先级> 是一个整数,在自动模式下,这个数字越高的选项,其优先级也就越高。
 
选项:
  --altdir <目录>          改变候选项目录。
  --admindir <目录>        设置 statoverride 文件的目录。
  --log <文件>             改变日志文件。
  --force                  就算没有通过自检,也强制执行操作。
  --skip-auto              在自动模式中跳过设置正确候选项的提示
                           (只与 --config 有关)
  --verbose                启用详细输出。
  --quiet                  安静模式,输出尽可能少的信息。不显示输出信息。
  --help                   显示本帮助信息。
  --version                显示版本信息。
Copy after login

--install : Create a Group candidates

--config : Configure the options in the group and choose which one to use

--remove : Remove the option from

First let’s see if there are any options for Python:

$ update-alternatives --display pythonupdate-alternatives: 错误: 无 python 的候选项
Copy after login

Then first create python group, and add the options of Python2 and Python3

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2 # 添加Python2可选项,优先级为2
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1 #添加Python3可选项,优先级为1
Copy after login

Note that in the /usr/bin/python link file here, the two options must be the same, so that this link file can select two Different options to link.

If we look at the file /usr/bin/python at this time, we will find that it is already linked to /etc/alternatives/python.

lrwxrwxrwx 1 root root        24  6月 19 18:39 python -> /etc/alternatives/python
Copy after login

Then let’s take a look at the version

$ python --version
Python 2.7.6
Copy after login

Why is it still Python2? Take a look at the configuration

$ sudo update-alternatives --config python
有 2 个候选项可用于替换 python (提供 /usr/bin/python)。   
选择         路径           优先级     状态
------------------------------------------------------------
* 0     /usr/bin/python2.7       2      自动模式  
  1     /usr/bin/python2.7       2      手动模式  
  2     /usr/bin/python3.4       1      手动模式
要维持当前值[*]请按回车键,或者键入选择的编号:
Copy after login

It turns out that it’s because the automatic mode is selected by default, and Python2 has a higher priority than Python3, just type 2 at this time to use Python3.

If you want to remove an option:

$ sudo update-alternatives --remove python /usr/bin/python2.7
Copy after login

update-alternatives only applies to Debian-based Linux.

4 virtualenvwrapper Switch version

virtualenvwrapper is a tool for managing Python virtual environments. It can easily create independent environments for different projects. Each project can be installed own dependencies, and also supports the existence of different versions of Python in different virtual environments.

First install virtualenvwrapper, you can choose apt installation or pip installation

apt installation

$ sudo apt-get install virtualenvwrapper
Copy after login

pip installation

$ sudo pip install virtualenvwrapper
Copy after login

When you need to use Python2 to develop projects, Create a Python2 virtual environment:

$ mkvirtualenv -p /usr/bin/python2 env27
Copy after login

When you need Python3 development:

$ mkvirtualenv -p /usr/bin/python3.4 env34
Copy after login

Then you can switch to different virtual environments at any time:

$ workon env27  # 进入Python2环境$ workon env34  # 进入Python3环境
Copy after login

It’s more fun Yes, you can switch to the project directory while entering the virtual environment. You only need to edit the file $VIRTUAL_ENV/bin/postactivate:

$ vim $VIRTUAL_ENV/bin/postactivate  #前提是已经进入对应的虚拟环境
Copy after login

Add the command to switch directories in the file:

cd  /path/to/your/project
Copy after login

5 Summary

The first two methods are not recommended.

Using update-alternatives to switch versions is only applicable to Debian-based Linux.

It is recommended to use virtualenvwrapper to manage virtual environments and versions.

The above is the detailed content of How to switch python2 and python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template