Introduction of Python Multi version Development Environment Management Tool

  • 2021-07-09 08:36:03
  • OfStack

Preface

In Python development, there are situations where we may face the need to install multiple versions of Python on one machine at a time. For example:

There are multiple Python projects, and each project depends on a different version of Python.

There is an Python project, which needs to support multiple Python versions at the same time.

So, how to efficiently install and maintain multiple versions of Python (specifically, Python here refers to Python interpreter) on a single machine?

In addition, we may face the need to install multiple versions of an Python third-party library on one machine. For example, there are multiple Python projects, each relying on a different version of the Python third-party library, requests. At this time, how to implement the installation and maintenance of multi-version Python requests library on a single machine?

This paper introduces an artifact. It provides the simplest way to meet the above two requirements at the same time.

Multi-version Python management

The tool for implementing multi-version Python management is called pyenv. Its installation command is:


curl https://pyenv.run | bash

After the installation is complete, simple configuration is required. Add the following lines of configuration information to the file ~/. bashrc, and then execute the command exec "$SHELL" to make the configuration work.


export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Next, you can check whether the installation and configuration were successful by looking at the pyenv version:


root@hzettv53:~# pyenv -v
pyenv 1.2.12

Because pyenv is based on source code, Python is compiled and installed. Therefore, we need to install and compile 1 dependent packages first. Because these dependencies are operating system related. Therefore, different operating systems have different installation commands.

Taking the common Ubuntu/Debian system as an example, the installation command is:


sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

You can now install Python using pyenv. Before installing the new version, let's check the Python version currently installed on the system:


root@hzettv53:~# pyenv versions
* system (set by /root/.pyenv/version)
root@hzettv53:~# python -V
Python 2.7.12

As you can see, you are currently using Python, which comes with the operating system, and its version is 2.7. 12. At this point, if we need to install a new version, such as Python 3.7. 2, simply execute the command:


pyenv install -v 3.7.2

Note that since pyenv is compiled and installed based on Python source code, this 1 step is a bit slow and requires patience. After installation, check the Python version in your system:


root@hzettv53:~# pyenv versions
* system (set by /root/.pyenv/version)
3.7.2
root@hzettv53:~# python -V
Python 2.7.12

As you can see, there are already two versions of Python in the system. However, the current system version is still used. If you want to use the newly installed version, just:


root@hzettv53:~# pyenv global 3.7.2
root@hzettv53:~# pyenv versions
system
* 3.7.2 (set by /root/.pyenv/version)
root@hzettv53:~# python -V
Python 3.7.2

The command pyenv global 3.7. 2 here changes the global version of Python. If you only want to use Python 3.7. 2 in the current folder, you can execute: pyenv local 3.7. 2; If you only want to use Python 3.7. 2 in the current Shell environment, you can execute: pyenv shell 3.7. 2.

If global, local and shell are set up at the same time, which one will really take effect? There is one priority sort, shell > local > global. For example, if pyenv local 3.7. 2 and pyenv global 3.7. 3 are executed, the current Python version in effect is 3.7. 2 because local has higher priority than global.

In this way, we can easily install multiple versions of Python on a single machine, and can flexibly switch Python versions according to actual needs.

Multi-virtual environment management

Not only may the dependent version of Python differ from project to project, but the dependent version of Python third-party library may also differ. We call Python and its third repository 1, which the project depends on, a virtual environment. If there are multiple Python projects at the same time, each relying on different virtual environments, how to manage them effectively?

At this time, we can still use pyenv to achieve our goals. Use the following command to create a virtual environment with a specified version of Python.


pyenv virtualenv <python_version> <environment_name>

The name of the virtual environment is recommended to reflect the Python project name. For example, if we have a project called myproject and are developing based on Python 3.7. 2, we can execute the command:


pyenv virtualenv 3.7.2 myprojectenv

In this way, we created a virtual environment named myprojectenv. So how to use it? Where we need to use this virtual environment (for example, the folder path from cd to project myproject), we can execute the following command:


export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
0

At this point, we can see that both Python and pip currently in use point to the virtual environment myprojectenv:


export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
1

This means that all the Python 3rd packages we installed with the pip command at this time are installed in the path of the virtual environment myprojectenv, not the system path. In this way, we bind the Python project to the Python development environment on which it depends (that is, the virtual environment). Different Python projects can use different Python virtual environment without affecting each other.

So, what if an Python project requires two Python virtual environments? We only need to create two virtual environments (for example, myprojectenv and myprojectenv2) and switch them when we use them:


export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
2

Summarize

Efficient management of development environment is a difficult problem in software engineering, especially when it is necessary to maintain multiple environments at the same time. pyenv provides efficient management of multiple versions of Python and multiple types of Python virtual environments on a single machine. pyenv is easy to use and easy to use.

github Address


Related articles: