Describes how to manage multiple Python versions using the pyenv command.

Source: Internet
Author: User
Tags rehash virtualenv
This article describes how to use the pyenv command to manage multiple Python dependent environments, for more information about how to use the pyenv command to manage multiple # wiki/1514.html "target =" _ blank "> environment-dependent Python versions, for more information, see

Since accessing Python, virtualenv and virtualenvwrapper have been used to manage the dependent environments of different projects. it is a pleasure to switch the virtual environment through commands such as workon and mkvirtualenv.

However, recently I want to make the project compatible with more Python versions. for example, if I want to be compatible with Python and Python + at least simultaneously, I found that the previous method would not work.

The biggest problem is that, after both Python and Python 3 are installed on the local computer, even if virtualenv and virtualenvwrapper are installed for the two Python versions respectively, the workon and mkvirtualenv commands of two Python versions cannot take effect at the same time. On the other hand, if you want to install multiple Python versions on the local computer, you will find that the installation cost is high and the implementation method is not elegant enough.

Fortunately, there is already a mature solution for this pain point, namely pyenv.

The following is an official introduction.

pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.This project was forked from rbenv and ruby-build , and modified for Python.

This article introduces pyenv's core functions.

Basic principles

If you want to explain the working principle of pyenv, you can simply use one sentence, that is, to modify the PATH of the system environment variable.

The system environment variable PATH contains a string of paths separated by colons, such as/usr/local/bin:/usr/bin:/bin. Every time you execute a command in the system, such as python or pip, the operating system will find the corresponding command from left to right in all paths of the PATH. Because the search is performed sequentially, the path on the left has a higher priority.

Pyenv inserts a $ (pyenv root)/shims directory at the beginning of the PATH. In this way, pyenv can flexibly switch to the desired Python version by controlling the Python version number in the shims directory.

For more details, see the pyenv documentation and its source code implementation.

Environment initialization

Pyenv has multiple installation methods. we recommend using pyenv-installer for the following reasons:

With pyenv-installer, you can install the pyenv bucket with one click, which can be easily upgraded in the future;
The installation method of pyenv-installer is based on GitHub, which ensures that the latest version of pyenv is always used, and the Python version Library is the latest and most complete.
Install & config

Run the following command to install the pyenv bucket.

$ curl -L | bash

In addition to pyenv, the content also includes the following plug-ins:

  1. Pyenv-doctor

  2. Pyenv-installer

  3. Pyenv-update

  4. Pyenv-virtualenv

  5. Pyenv-which-ext

After the installation is complete, the pyenv command has not added the system environment variables. you need to add the following content ~ /. Zshrc, and then execute source ~ /. Zshrc.

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

After completing the preceding steps, pyenv is installed.

$ pyenv -vpyenv 1.0.8

If you are not sure whether the pyenv environment is correctly installed, you can use the pyenv doctor command to check the environment.

$ pyenv doctorCloning /Users/Leo/.pyenv/plugins/pyenv-doctor/bin/.....Installing python-pyenv-doctor...BUILD FAILED (OS X 10.12.3 using python-build 20160602)Last 10 log lines:checking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking openssl/ssl.h usability... nochecking openssl/ssl.h presence... nochecking for openssl/ssl.h... noconfigure: error: OpenSSL development header is not installed.make: *** No targets specified and no makefile found. Stop.Problem(s) detected while checking system.

Through detection, you can find possible problems in the local environment. for example, from the above output, we can see that the local OpenSSL development header has not been installed. Fix the problem one by one as prompted until the problem does not occur during detection.


You can use the pyenv update command to update all contents of the pyenv family bucket.

$ pyenv updateUpdating /Users/Leo/.pyenv...From * branch      master   -> FETCH_HEADAlready up-to-date.Updating /Users/Leo/.pyenv/plugins/pyenv-doctor...From * branch      master   -> FETCH_HEADAlready up-to-date.Updating /Users/Leo/.pyenv/plugins/pyenv-installer...From * branch      master   -> FETCH_HEADAlready up-to-date.Updating /Users/Leo/.pyenv/plugins/pyenv-update...From * branch      master   -> FETCH_HEADAlready up-to-date.Updating /Users/Leo/.pyenv/plugins/pyenv-virtualenv...From * branch      master   -> FETCH_HEADAlready up-to-date.Updating /Users/Leo/.pyenv/plugins/pyenv-which-ext...From * branch      master   -> FETCH_HEADAlready up-to-date.

Core usage of pyenv

The main functions of pyenv are as follows:

$ Pyenv-h
Usage: pyenv[]

Some useful pyenv commands are:
Commands List all available pyenv commands
Local Set or show the local application-specific Python version
Global Set or show the global Python version
Shell Set or show the shell-specific Python version
Install Install a Python version using python-build
Uninstall Uninstall a specific Python version
Rehash Rehash pyenv shims (run this after installing executables)
Version Show the current Python version and its origin
Versions List all Python versions available to pyenv
Which Display the full path to an executable
Whence List all Python versions that contain the given executable

See 'pyenv help'For information on a specific command.
For full documentation, see:

View all installed Python versions

$ pyenv install --listAvailable versions: 2.1.3 ... 2.7.12 2.7.13 ... 3.5.3 3.6.0 3.6-dev 3.6.1 3.7-dev

Note that if you use the brew command to install pyenv, you may find that the Python version library does not have the latest Python version. Therefore, it is recommended to install pyenv through GitHub source code.

Install the Python environment of the specified version

$ pyenv install 3.6.0Downloading Python-3.6.0.tar.xz...-> Python-3.6.0...Installed Python-3.6.0 to /Users/Leo/.pyenv/versions/3.6.0

View all available Python versions in the current system

$ pyenv versions* system (set by /Users/Leo/.pyenv/version) 2.7.13 3.6.0

Switch Python version

Pyenv can manage the Python environment in three dimensions: current system, current directory, and current shell. The priority of these three dimensions increases from left to right, that is, the current system has the lowest priority and the current shell has the highest priority.

If you want to modify the global Python environment of the system, you can use the pyenv global PYTHON_VERSION command. After this command is executed, it will be in the $ (pyenv root) directory (default ~ /. Pyenv) to create a file named version (if the file already exists, modify the content of the file), which records the global Python version number of the system.

$ pyenv global 2.7.13$ cat ~/.pyenv/version2.7.13$ pyenv version2.7.13 (set by /Users/Leo/.pyenv/version)$ pyenv global 3.6.0$ cat ~/.pyenv/version3.6.0$ pyenv version3.6.0 (set by /Users/Leo/.pyenv/version)

In general, for specific projects, we may need to switch to different Python environments. at this time, we can use the pyenv local PYTHON_VERSION command to modify the Python environment of the current directory. After the command is executed,. python-version File (if the file already exists, modify the file content), which records the Python version used in the current directory.

$ cat ~/.pyenv/version2.7.13$ pyenv local 3.6.0$ cat .python-version3.6.0$ cat ~/.pyenv/version2.7.13$ pyenv version3.6.0 (set by /Users/Leo/MyProjects/.python-version)$ pip -Vpip 9.0.1 from /Users/Leo/.pyenv/versions/3.6.0/lib/python3.6/site-packages (python 3.6)

We can see that the. python-version configuration in the current directory takes precedence over the global system ~ /. Pyenv/version configuration.

In another case, you can run the pyenv shell PYTHON_VERSION command to modify the Python environment of the current shell. After executing this command, an environment variable named PYENV_VERSION is created in the current shell session (Terminal window, then, the Python version set by this environment variable will be used in any Directory of the current shell. At this time, the Python version set in the current system and the current directory will be ignored.

$ echo $PYENV_VERSION$ pyenv shell 3.6.0$ echo $PYENV_VERSION3.6.0$ cat .python-version2.7.13$ pyenv version3.6.0 (set by PYENV_VERSION environment variable)

As the name suggests, the Python environment of the current shell only takes effect in the current shell. after a new shell is opened, the environment becomes invalid. To cancel the shell-level Python environment in the current shell, use the unset command to reset the PYENV_VERSION environment variable.

$ cat .python-version2.7.13$ pyenv version3.6.0 (set by PYENV_VERSION environment variable)$ unset PYENV_VERSION$ pyenv version2.7.13 (set by /Users/Leo/MyProjects/.python-version)

Manage multiple dependent database environments

After the above operations, we can install multiple Python runtime environments on the local computer, and flexibly switch according to actual needs. However, in many cases, in the same Python version, we still want to separate the environment based on the project, just like using virtualenv.

Pyenv also contains such a plug-in. pyenv-virtualenv can implement the same function.

The usage is as follows:


Here, PYTHON_VERSION is the Python version. for example, 3.6.0 and PROJECT_NAME are the custom project names. A good practice is to include the Python version number in PROJECT_NAME to facilitate identification.

Suppose we have an XDiff project and want to create a virtual environment for Python 2.7.13 and Python 3.6.0 respectively, then we can execute the following commands in sequence.

$ pyenv virtualenv 3.6.0 py36_XDiff$ pyenv virtualenv 2.7.13 py27_XDiff

After the project is created, run the pyenv virtualenvs command to view all the local project environments.

$ pyenv virtualenvs 2.7.13/envs/py27_XDiff (created from /Users/Leo/.pyenv/versions/2.7.13)* 3.6.0/envs/py36_XDiff (created from /Users/Leo/.pyenv/versions/3.6.0) py27_XDiff (created from /Users/Leo/.pyenv/versions/2.7.13) py36_XDiff (created from /Users/Leo/.pyenv/versions/3.6.0)

In this way, we can also create multiple virtual environments in the same Python version, and then maintain the dependent database environment in each virtual environment.

For example, the py36_XDiff virtual environment is located in/Users/Leo /. pyenv/versions/3.6.0/envs directory, and the dependent Library is located in/Users/Leo /. in pyenv/versions/3.6.0/lib/python3.6/site-packages.

$ pip -Vpip 9.0.1 from /Users/Leo/.pyenv/versions/3.6.0/lib/python3.6/site-packages (python 3.6)

Later in the project development process, we can use the pyenv local XXX or pyenv activate PROJECT_NAME command to switch the Python environment of the project.

➜ MyProjects pyenv local py27_XDiff(py27_XDiff) ➜ MyProjects pyenv versionpy27_XDiff (set by /Users/Leo/MyProjects/.python-version)(py27_XDiff) ➜ MyProjects python -VPython 2.7.13(py27_XDiff) ➜ MyProjects pip -Vpip 9.0.1 from /Users/Leo/.pyenv/versions/2.7.13/envs/py27_XDiff/lib/python2.7/site-packages (python 2.7)

As you can see, after the environment is switched, the directory corresponding to the pip command also changes, that is, it always corresponds to the current Python virtual environment.

Use the pyenv deactivate command to exit the Python virtual environment of the current project.

To remove a project environment, run the following command.

$ pyenv uninstall PROJECT_NAME

The above are commonly used pyenv commands in daily development work, which can basically meet the needs of the vast majority of dependent Library environment management.

The above is a detailed description of how the pyenv command manages multiple Python versions. For more information, see other related articles in the first PHP community!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.