Home>Article>Backend Development> How to switch python2 and python3

How to switch python2 and python3

Barbara Streisand
Barbara Streisand Original
2019-06-19 16:36:38 6462browse

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

Then we modify the alias

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

/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

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 显示版本信息。

--install 52a25b17b52424c09a9e188108722f11 af0c92e9f9d1400e4fba6230747949fe 91708595e1835e2bc8ff888c56372a26 d93d81cdd12b442d2a982f70506a8b23 : Create a Group candidates

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

--remove 8a11bc632ea32a57b3e3693c7987c420 91708595e1835e2bc8ff888c56372a26: Remove the 91708595e1835e2bc8ff888c56372a26 option from af0c92e9f9d1400e4fba6230747949fe

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

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

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

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

Then let’s take a look at the version

$ python --version Python 2.7.6

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 手动模式 要维持当前值[*]请按回车键,或者键入选择的编号:

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

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

pip installation

$ sudo pip install virtualenvwrapper

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

$ mkvirtualenv -p /usr/bin/python2 env27

When you need Python3 development:

$ mkvirtualenv -p /usr/bin/python3.4 env34

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

$ workon env27 # 进入Python2环境$ workon env34 # 进入Python3环境

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 #前提是已经进入对应的虚拟环境

Add the command to switch directories in the file:

cd /path/to/your/project

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!

Statement:
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