Linux, Python version of several ways to switch

This post for Linux users, through testing in Ubuntu

0 Why do we need two versions of Python

Python2 and Python3 is not compatible with each contact with the Python developers know something, though Python3 is the future, but there are still many projects using Python2 development. Many Linux distributions (such as Ubuntu) will be built Python2.7, but when we are ready to develop a Python3 project, how can we do?

Then the Python3 also slightly lower. Ah, under Linux can indeed two packed together, but the question is how you switch two versions of Python.

1 modify the alias

First, we first look at the default version of Python

$ python --version
Python 2.7.6 

Then we modify alias

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

/usr/bin/python3This path is how to find it?

In general, software binaries are available on /usr/binor /usr/local/bin(the high priority it) found. Of course, if you are a Debian-based Linux, so to find (provided that you have installed Python3):

$ dpkg -L python3

The above modification is temporary alias, a window configuration reopened disappeared. If you want to use this alias every window, you can edit ~/.bashrc(if you are another shell, then it is not the file, such as zsh is ~/.zshrc), the alias write the configuration file.

Modify the alias advantage is simple enough, but the switch is not flexible.

2 linked files

In /usr/binestablishing a link file pointing to Python3 in.

$ ln -s python /usr/bin/python3
$ python --version Python 3.4.3 

With modifications as an alias, but also flexible enough to modify.

3 uses switched version update-alternatives

update-alternatives is a tool provided by Debian (non-Debian system do not read), a principle similar to the way top, is also linked by the way, but the process of switching its very convenient.

First look at Help the 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 显示版本信息。 

We only need to know three parameters on the line

  • --install <链接> <名称> <路径> <优先级>: Establish a set of candidates
  • --config <名称>: Configuration <name> group option, and choose which of
  • --remove <名称> <路径>: Remove <path> option from <name>

First we look at have no option about Python:

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

That first group to establish a python, and add Python2 and Python3 of options

$ 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 here the /usr/bin/pythonlinked file, two options must be the same, so the link files can choose two different options to go to link.

Then if we look at /usr/bin/pythonwhen this file, you will find that it has links to /etc/alternatives/python.

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

Then we'll see versions

$ python --version
Python 2.7.6 

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

The original is because the automatic mode is selected by default, but Python2 higher priority than Python3, this time as long as the type 2, you can use the Python3.

If you want to remove an option, then:

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

update-alternatives applies only Debian based Liunx.

4 virtualenvwrapper switching version

virtualenvwrapper Python virtual environment management tools, you can easily set up an independent environment for different projects, each project can install their own dependence, also supports Python there are different versions in different virtual environments.

First mounting virtualenvwrapper, or can choose to install apt mounted pip

apt installation

$ sudo apt-get install virtualenvwrapper

pip install

$ sudo pip install virtualenvwrapper

When you need to use Python2 development projects, the establishment of a virtual environment of a Python2:

$ mkvirtualenv -p /usr/bin/python2 env27

When you need Python3 Development:

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

You can then switch between different virtual environments at any time:

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

Gengshuang is that you can switch into the virtual environment at the same time to the project directory, only need to edit $VIRTUAL_ENV/bin/postactivatethis file to:

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

Add command to switch directories in the file:

cd  /path/to/your/project

5 summary

The first two methods are not recommended.

Use update-alternatives switch is only for Linux Debian system.

Virtualenvwrapper recommended to manage the virtual environment and version.

Further, the method of switching the different versions of the software described herein, in addition to virtualenvwrapper, the foregoing three methods are applicable to other software, such as Java (open-jdk and oracle-jdk).

Guess you like

Origin www.cnblogs.com/feynxd/p/11367806.html