Detailed pyenv command to manage multiple Python version methods

Source: Internet
Author: User
Tags rehash virtual environment virtualenv
This article mainly describes the PYENV command to manage multiple http://www.php.cn/wiki/1514.html "target=" _blank ">python version dependent on the environment of the relevant information, the need for friends can refer to the following

Since the contact with Python, has been the use of virtualenv and virtualenvwrapper to manage the dependent environment of different projects, through the Workon, mkvirtualenv and other commands to switch the virtual environment, it is very enjoyable.

However, the recent desire to make the project compatible with more Python versions, such as at least simultaneously compatible with Python2.7 and python3.3+, found that the previous approach did not work.

The biggest problem is that after the local computer installs both Python2.7 and Python3, virtualenv and Virtualenvwrapper are installed for two Python versions, and two Python versions of Workon, MKV The irtualenv command takes effect at the same time. On the other hand, if you want to install multiple versions of Python on your local computer, you will find that the installation cost is high and the implementation is not elegant.

Fortunately, for this pain point, there is already a relatively mature solution, that is 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-do-one thing well. This project is forked from Rbenv and Ruby-build, and modified for Python.

This article is about the core functions of pyenv.

Basic principle

If you want to explain how pyenv works, basically a sentence can be summed up, that is: Modify the system environment variable PATH.

For the system environment variable path, I believe everyone is not unfamiliar, it contains a string separated by a colon path, such as/usr/local/bin:/usr/bin:/bin. Whenever a command is executed in the system, such as Python or PIP, the operating system looks for the corresponding command from left to right in all paths in path. because it is searched in turn, the path to the left has a higher priority.

What pyenv do is to insert a $ (pyenv root)/shims directory at the front of the PATH. This allows pyenv to flexibly switch to the Python version we need by controlling the Python version number in the Shims directory.

If you want to know more details, you can see the pyenv documentation and its source code implementation.

Environment initialization

Pyenv installation methods include a variety, the focus is recommended the use of Pyenv-installer, the main reasons are two points:

Through the Pyenv-installer can be installed pyenv family barrels a button, follow-up can also be easily implemented one-click upgrade;
Pyenv-installer's installation is based on GitHub, ensuring that the latest version of Pyenv is always used, and that the Python repository is also up-to-date.
Install && Config

Install the Pyenv family bucket with the following command.

$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

The content includes the following plugins in addition to pyenv:

    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 been added to the system's environment variables, you need to add the following to ~/.ZSHRC, and then execute the source ~/.ZSHRC.

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

After completing the above operation, the PYENV installation is complete.

$ pyenv-vpyenv 1.0.8

If you are unsure whether the PYENV environment is installed properly, you can detect the environment through the Pyenv Doctor command.

$ 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 ten 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 Usabi Lity nochecking openssl/ssl.h presence ... nochecking for openssl/ssl.h ... Noconfigure:error:OpenSSL development head Er is not installed.make: * * * NO targets specified and no makefile found. Stop.problem (s) detected while checking system.

Detection allows you to identify possible problems with your local environment, for example, as you can see from the above output, the local OpenSSL development header is not installed. Depending on the problem you are prompted, fix it individually until the problem is no longer detected.

Update

With the pyenv Update command, you can update all the contents of the Pyenv family bucket.

$ pyenv updateupdating/users/leo/.pyenv ... From Https://github.com/yyuu/pyenv * Branch master, Fetch_headalready up-to-date. Updating/users/leo/.pyenv/plugins/pyenv-doctor ... From Https://github.com/yyuu/pyenv-doctor * Branch master, Fetch_headalready up-to-date. Updating/users/leo/.pyenv/plugins/pyenv-installer ... From Https://github.com/yyuu/pyenv-installer * Branch master, Fetch_headalready up-to-date. Updating/users/leo/.pyenv/plugins/pyenv-update ... From Https://github.com/yyuu/pyenv-update * Branch master, Fetch_headalready up-to-date. Updating/users/leo/.pyenv/plugins/pyenv-virtualenv ... From Https://github.com/yyuu/pyenv-virtualenv * Branch master, Fetch_headalready up-to-date. Updating/users/leo/.pyenv/plugins/pyenv-which-ext ... From Https://github.com/yyuu/pyenv-which-ext * Branch master, fetch_headalready up-to-date. 

How to use the core of pyenv

The main functions of pyenv are as follows:

$ pyenv-h
Usage:pyenv <command> [<args>]

Some useful pyenv Commands is:
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 <command>" For information on a specific command.
For full documentation, SEE:HTTPS://GITHUB.COM/YYUU/PYENV#README

View all versions of Python that can be installed

$ 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

It is important to note that if you install pyenv with the Brew command, you may find that there is no latest Python version in the Python repository. Therefore, it is recommended to install pyenv via GitHub source mode.

Install the specified version of the Python environment

$ pyenv Install 3.6.0Downloading python-3.6.0.tar.xz...-> https://www.python.org/ftp/python/3.6.0/ Python-3.6.0.tar.xzinstalling 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

Toggle Python Version

Pyenv can manage the Python environment from three dimensions, for short: current system, current directory, current Shell. The priority of these three dimensions is raised 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 system global PYTHON environment, you can use the PYENV Global python_version command. When the command executes, a file named version is created in the (pyenv root) directory (default = ~/.pyenv), and if the file already exists, the contents of the file are modified, and the system global Python version number is recorded.

$ 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)

Typically, for a particular project, we may need to switch to a different Python environment, and at this point we can modify the current directory's Python environment by pyenv the local python_version command. After the command executes, a. python-version file is generated in the current directory (if the file already exists, the contents of the file are modified), and the Python version number used in the current directory is recorded.

$ 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)

As you can see, the. python-version configuration in the current directory takes precedence over the system global ~/.pyenv/version configuration.

In another case, you can modify the current shell's PYTHON environment by executing the pyenv Shell python_version command. After executing the command, an environment variable named Pyenv_version is created in the current Shell session (terminal window), and then in any directory in the current shell, the Python version set for that environment variable is used. At this point, both the current system and the Python version set in the current directory are 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 implies, the current shell's Python environment only takes effect in the current shell, and once a new shell is reopened, the environment is invalidated. If you want 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 ve rsion2.7.13 (set By/users/leo/myprojects/.python-version)

Managing multiple Dependent library environments

With this in place, we can install multiple versions of the Python runtime environment on the local computer and have the flexibility to switch between the actual requirements. However, many times in the same Python version, we still want to be able to separate the environment according to the project, just like we used virtualenv.

In Pyenv, also contains such a plug-in, pyenv-virtualenv, can achieve the same function.

Use the following methods:

$ pyenv virtualenv PYTHON_VERSION PROJECT_NAME

Where Python_version is the specific PYTHON version number, for example, 3.6.0, Project_Name is our custom project name. A good practice is to also take Python's version number in Project_Name for easy identification.

Now that we have a xdiff project that wants to create a virtual environment for Python 2.7.13 and Python 3.6.0, you can execute the following command in turn.

$ pyenv virtualenv 3.6.0 py36_xdiff$ pyenv virtualenv 2.7.13 Py27_xdiff

Once created, you can see all the local project environments by executing the pyenv Virtualenvs command.

$ 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 under the same Python version, and then maintain the dependent library environment in each virtual environment separately.

For example, the Py36_xdiff virtual environment is located in the/users/leo/.pyenv/versions/3.6.0/envs directory, and its dependent libraries are located in/users/leo/.pyenv/versions/3.6.0/lib/ The 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 switch the Python environment of the project by pyenv the local XXX or pyenv activate project_name command.

➜myprojects pyenv Local Py27_xdiff (py27_xdiff) ➜myprojects pyenv Versionpy27_xdiff (set By/users/leo/myprojects/.pytho n-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 switching the environment, the corresponding directory of the PIP command changes, which always corresponds to the current Python virtual environment.

Corresponding to the Python virtual environment that uses the Pyenv deactivate command to exit the current project.

If you want to remove a project environment, you can do so by using the following command.

$ pyenv uninstall PROJECT_NAME

The above is the daily development work commonly used in the pyenv command, basically can meet the overwhelming majority of dependent library environment management needs.

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: info-contact@alibabacloud.com 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.