preface

In Python development, there may be situations where you need to install multiple versions of Python on a single machine at the same time. Such as:

  • There are multiple Python projects, each relying on a different version of Python.
  • There is a Python project that needs to support multiple Versions of Python simultaneously.

So how do you efficiently install and maintain multiple versions of Python(specifically, Python means the Python interpreter) on a single machine?

In addition, we may face the need to install multiple versions of a Python third-party library on a single machine. For example, there are multiple Python projects, each relying on a different version of Python’s third-party library Requests. How do you install and maintain multiple versions of the Python Requests library on a single machine?

This article introduces a magic ware. It provides the simplest way to satisfy both of these requirements.

Multi-version Python management

The tool that implements multiple versions of Python management is called PyEnv. Its installation commands are:

curl https://pyenv.run | bash
Copy the code

After the installation is complete, perform simple configuration. Add the following lines of configuration information to the file ~/.bashrc, and then execute exec “$SHELL” for the configuration to take effect.

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

Next, you can check that the installation and configuration is successful by looking at the PyEnv version:

root@hzettv53:~# pyenv -vPyenv 1.2.12Copy the code

Since PyEnv is compiled and installed in Python based on source code. Therefore, we need to install some compile-related dependencies first. Because these dependencies are operating system specific. Therefore, the installation commands vary with the operating system.

For the common Ubuntu/Debian OS, run the following command:

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
Copy the code

You are now ready to install Python using PyEnv. Before installing the new version, let’s look at 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
Copy the code

The current version of the Python operating system 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:

3.7.2 pyenv install - vCopy the code

Note that since PyEnv is compiled and installed from Python source code, this step is a bit slow and requires patience. After the installation is complete, check the Python version on the system:

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

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

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
Copy the code

Here the command pyenv global 3.7.2 changes the global Python version. 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 your current Shell environment, you can execute: Pyenv Shell 3.7.2.

If you set global, local, and shell versions of Python at the same time, which one will actually work? There is a priority order, shell > local > global. For example, if Pyenv local 3.7.2 and PyEnv Global 3.7.3 are executed, the current Python version is 3.7.2 because local takes precedence over global.

This makes it easy to install multiple versions of Python on a single machine and flexibly switch between Python versions as needed.

Manage multiple virtual environments

Not only may the versions of Python that you rely on differ from project to project, but also the versions of Python third-party libraries that you rely on. We collectively refer to Python and its third-party libraries on which the project depends as virtual environments. How can you manage multiple Python projects, each dependent on a different virtual environment, effectively?

At this point, we can still use PyEnv for our purposes. Use the following command to create a virtual environment that specifies the Python version.

pyenv virtualenv <python_version> <environment_name>
Copy the code

The name of the virtual environment is recommended to indicate the Python project name. For example, if we have a project called MyProject, developed based on Python 3.7.2, we can execute the following command:

Pyenv virtualenv 3.7.2 myprojectenvCopy the code

So we created a virtual environment called MyProjectenv. So how do you use it? Where we need to use the virtual environment (for example, CD to the folder path of the project myProject), we can simply execute the following command:

pyenv local myprojectenv
Copy the code

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

root@hzettv53:~/workspace/test# pyenv which python
/root/.pyenv/versions/myprojectenv/bin/python
root@hzettv53:~/workspace/test# pyenv which pip
/root/.pyenv/versions/myprojectenv/bin/pip
Copy the code

This means that all Python third-party packages that we installed with the PIP command at this point were installed in the path of the virtual environment myProjectenv, not the system path. This allows us to bind the Python project to the Python development environment (the virtual environment) on which it depends. Different Python projects can use different Python virtual environments without affecting each other.

So what if a Python project needs to use both Python virtual environments? We just need to create two virtual environments (such as MyProjectenv and myProjectenv2) and switch them when we use them:

root@hzettv53:~/workspace/myproject# pyenv local myprojectenv
root@hzettv53:~/workspace/myproject:~# python -V
Python 3.7.2
root@hzettv53:~/workspace/myproject# pyenv local myprojectenv2
root@hzettv53:~/workspace/myproject:~# python -V
Python 2.7.12
Copy the code

conclusion

Efficient management of development environments is a difficult problem in software engineering, especially when multiple environments need to be maintained simultaneously. Pyenv provides efficient management of multiple versions of Python and multiple types of Python virtual environments on a single machine. Pyenv is simple to use and easy to get started. To learn more about PyEnv, read:

  • Realpython.com/intro-to-py…
  • github.com/pyenv/pyenv