Detailed explanation of how to manage multiple Python versions using the pyenv command

高洛峰
Release: 2017-03-28 09:30:45
Original
1719 people have browsed it

This article mainly introduces the pyenv command to manage multiple Python versions that depend on the environment For related information, friends in need can refer to

Since I came into contact with Python, I have always used virtualenv and virtualenvwrapper to manage the dependent environments of different projects. It is very pleasant to switch virtual environments through commands such as workon and mkvirtualenv.

However, recently I wanted to make the project compatible with more Python versions, for example, at least compatible with Python2.7 and Python3.3+, but I found that the previous method did not work.

Maximum. The problem is that after installing Python2.7 and Python3 at the same time on the local computer, even if virtualenv and virtualenvwrapper are installed for the two Python versions respectively, the workon and mkvirtualenv commands of the two Python versions cannot take effect at the same time. On the other hand, if you want to install multiple Python versions on your local computer, you will find that the installation cost is relatively high and the implementation method is not elegant enough. Fortunately, there is already a relatively mature solution for this pain point. The solution is pyenv.

The following is the 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.
Copy after login

This article introduces the core functions of pyenv.

##Basic principles.

If you want to explain the working principle of pyenv, it can be summarized in one sentence, that is: modify the system environment variable PATH

For the system environment variable PATH, I believe everyone is familiar with it. It contains a string of paths separated by colons, such as /usr/local/bin:/usr/bin:/bin. Whenever a command is executed in the system, such as python or pip, the operating system The corresponding commands will be searched from left to right in all paths in PATH. Because they are searched in sequence, the paths on the left have higher priority.

What pyenv does is in PATH. Insert a $(pyenv root)/shims directory at the front. In this way, pyenv can flexibly switch to the Python version we need by controlling the Python version number in the shims directory.

If you still want to know more. For more details, you can view the documentation of pyenv and its source code implementation

Environment initialization

There are many ways to install pyenv, and pyenv-installer is recommended. method, there are two main reasons:

You can install pyenv family bucket with one click through pyenv-installer, and you can also easily upgrade it with one click in the future; The installation method of pyenv-installer is based on GitHub, which can Ensure that you always use the latest version of pyenv, and the Python version library is also the latest and most complete.

install && config

Install the pyenv family bucket through the following command.



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

Contents except include In addition to pyenv, it also includes the following plug-ins:

pyenv-doctor

  1. pyenv-installer

  2. pyenv-update

  3. pyenv-virtualenv

  4. pyenv-which-ext

  5. After installation is complete , The pyenv command has not been added to the system environment variables. You need to add the following content to ~/.zshrc, and then execute source ~/.zshrc.

    export PATH=$HOME/.pyenv/bin:$PATH
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    Copy after login
    After completing the above operations, pyenv is installed.
  6. $ pyenv -v
    pyenv 1.0.8
    Copy after login
If you are not sure whether the pyenv environment is installed properly, you can use the pyenv doctor command to check the environment.

$ pyenv doctor
Cloning /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... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking openssl/ssl.h usability... no
checking openssl/ssl.h presence... no
checking for openssl/ssl.h... no
configure: error: OpenSSL development header is not installed.
make: *** No targets specified and no makefile found. Stop.
Problem(s) detected while checking system.
Copy after login
Through detection, you can find possible problems in the local environment. For example, as can be seen from the above output, the local OpenSSL development header has not been installed. According to the prompted problems, repair them one by one until the problem no longer occurs.

update

Through the pyenv update command, you can update all the contents of the pyenv family bucket.

$ pyenv update
Updating /Users/Leo/.pyenv...
From https://github.com/yyuu/pyenv
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Updating /Users/Leo/.pyenv/plugins/pyenv-doctor...
From https://github.com/yyuu/pyenv-doctor
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Updating /Users/Leo/.pyenv/plugins/pyenv-installer...
From https://github.com/yyuu/pyenv-installer
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Updating /Users/Leo/.pyenv/plugins/pyenv-update...
From https://github.com/yyuu/pyenv-update
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Updating /Users/Leo/.pyenv/plugins/pyenv-virtualenv...
From https://github.com/yyuu/pyenv-virtualenv
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Updating /Users/Leo/.pyenv/plugins/pyenv-which-ext...
From https://github.com/yyuu/pyenv-which-ext
 * branch      master   -> FETCH_HEAD
Already up-to-date.
Copy after login
Core usage of pyenv

The main functions of pyenv are as follows:

$ pyenv -hUsage: 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: https://github.com/yyuu/pyenv#readme

查看所有可安装的 Python 版本

$ pyenv install --list
Available 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
Copy after login

需要注意的是,如果是采用 brew 命令安装的 pyenv ,可能会发现 Python 版本库中没有最新的 Python 版本。所以建议还是通过 GitHub 源码方式安装 pyenv 。

安装指定版本的 Python 环境

$ pyenv install 3.6.0
Downloading Python-3.6.0.tar.xz...
-> https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
Installing Python-3.6.0...
Installed Python-3.6.0 to /Users/Leo/.pyenv/versions/3.6.0
Copy after login

查看当前系统中所有可用的 Python 版本

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

切换 Python 版本

pyenv 可以从三个维度来管理 Python 环境,简称为: 当前系统 、 当前目录 、 当前shell 。这三个维度的优先级从左到右依次升高,即 当前系统 的优先级最低、 当前shell 的优先级最高。

如果想修改系统全局的Python环境,可以采用 pyenv global PYTHON_VERSION 命令。该命令执行后会在 $(pyenv root) 目录(默认为 ~/.pyenv )中创建一个名为 version 的文件(如果该文件已存在,则修改该文件的内容),里面记录着系统全局的Python版本号。

$ pyenv global 2.7.13
$ cat ~/.pyenv/version
2.7.13
$ pyenv version
2.7.13 (set by /Users/Leo/.pyenv/version)

$ pyenv global 3.6.0
$ cat ~/.pyenv/version
3.6.0
$ pyenv version
3.6.0 (set by /Users/Leo/.pyenv/version)
Copy after login

通常情况下,对于特定的项目,我们可能需要切换不同的Python环境,这个时候就可以通过 pyenv local PYTHON_VERSION 命令来修改 当前目录 的Python环境。命令执行后,会在当前目录中生成一个 .python-version 文件(如果该文件已存在,则修改该文件的内容),里面记录着当前目录使用的Python版本号。

$ cat ~/.pyenv/version
2.7.13
$ pyenv local 3.6.0
$ cat .python-version
3.6.0
$ cat ~/.pyenv/version
2.7.13
$ pyenv version
3.6.0 (set by /Users/Leo/MyProjects/.python-version)
$ pip -V
pip 9.0.1 from /Users/Leo/.pyenv/versions/3.6.0/lib/python3.6/site-packages (python 3.6)
Copy after login

可以看出,当前目录中的 .python-version 配置优先于系统全局的 ~/.pyenv/version 配置。

另外一种情况,通过执行 pyenv shell PYTHON_VERSION 命令,可以修改 当前shell 的Python环境。执行该命令后,会在当前 shell session (Terminal窗口)中创建一个名为 PYENV_VERSION 的环境变量,然后在 当前shell 的任意目录中都会采用该环境变量设定的Python版本。此时, 当前系统 和 当前目录 中设定的Python版本均会被忽略。

$ echo $PYENV_VERSION

$ pyenv shell 3.6.0
$ echo $PYENV_VERSION
3.6.0
$ cat .python-version
2.7.13
$ pyenv version
3.6.0 (set by PYENV_VERSION environment variable)
Copy after login

顾名思义, 当前shell 的Python环境仅在当前shell中生效,重新打开一个新的shell后,该环境也就失效了。如果想在 当前shell 中取消shell级别的Python环境,采用 unset 命令重置 PYENV_VERSION 环境变量即可。

$ cat .python-version
2.7.13
$ pyenv version
3.6.0 (set by PYENV_VERSION environment variable)

$ unset PYENV_VERSION
$ pyenv version
2.7.13 (set by /Users/Leo/MyProjects/.python-version)
Copy after login

管理多个依赖库环境

经过以上操作,我们在本地计算机中就可以安装多个版本的 Python 运行环境,并可以按照实际需求进行灵活地切换。然而,很多时候在同一个 Python 版本下,我们仍然希望能根据项目进行环境分离,就跟之前我们使用 virtualenv 一样。

在 pyenv 中,也包含这么一个插件, pyenv-virtualenv ,可以实现同样的功能。

使用方式如下:

$ pyenv virtualenv PYTHON_VERSION PROJECT_NAME

其中, PYTHON_VERSION 是具体的Python版本号,例如, 3.6.0 , PROJECT_NAME 是我们自定义的项目名称。比较好的实践方式是,在 PROJECT_NAME 也带上Python的版本号,以便于识别。

现假设我们有 XDiff 这么一个项目,想针对 Python 2.7.13 和 Python 3.6.0 分别创建一个虚拟环境,那就可以依次执行如下命令。

$ pyenv virtualenv 3.6.0 py36_XDiff
$ pyenv virtualenv 2.7.13 py27_XDiff
Copy after login

创建完成后,通过执行 pyenv virtualenvs 命令,就可以看到本地所有的项目环境。

$ 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)
Copy after login

通过这种方式,在同一个Python版本下我们也可以创建多个虚拟环境,然后在各个虚拟环境中分别维护依赖库环境。

例如, py36_XDiff 虚拟环境位于 /Users/Leo/.pyenv/versions/3.6.0/envs 目录下,而其依赖库位于 /Users/Leo/.pyenv/versions/3.6.0/lib/python3.6/site-packages 中。

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

后续在项目开发过程中,我们就可以通过 pyenv local XXX 或 pyenv activate PROJECT_NAME 命令来切换项目的 Python 环境。

➜ MyProjects pyenv local py27_XDiff
(py27_XDiff) ➜ MyProjects pyenv version
py27_XDiff (set by /Users/Leo/MyProjects/.python-version)
(py27_XDiff) ➜ MyProjects python -V
Python 2.7.13
(py27_XDiff) ➜ MyProjects pip -V
pip 9.0.1 from /Users/Leo/.pyenv/versions/2.7.13/envs/py27_XDiff/lib/python2.7/site-packages (python 2.7)
Copy after login

可以看出,切换环境后, pip 命令对应的目录也随之改变,即始终对应着当前的Python虚拟环境。

对应的,采用 pyenv deactivate 命令退出当前项目的 Python 虚拟环境。

如果想移除某个项目环境,可以通过如下命令实现。

$ pyenv uninstall PROJECT_NAME

以上便是日常开发工作中常用的 pyenv 命令,基本可以满足绝大多数依赖库环境管理方面的需求。

The above is the detailed content of Detailed explanation of how to manage multiple Python versions using the pyenv command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!