Stay Hungry,Stay Foolish!

pyenv - python多版本和多环境管理利器

背景

问题:

(1)同一个环境中,由于方案不同,选择的python版本不同。

一些开源软件,可能依赖老旧的python版本,但是项目选择的较新的python版本。

这样导致一个环境中,需要安装多个python版本的需求。

 

(2)即使对于同一python版本,多个不同的项目,选择的依赖包,有可能也有不同。

 

对于第一个问题,只能手动单独安装不同版本,例如通过 apt-get install python3.7

对于 python2 和 python3 的不同版本,则又需要维护 /usr/bin/python 和 /usr/bin/python3 和 pip软连接。

这是一件繁琐且重复的事情。

 

对于第二个问题, 可以采用 virtualenv 或者 pipenv 工具解决。 但是其必须是在某个指定的python版本上首先安装这些工具。

 

解决方案

pyenv 为这两个问题同时提供了解决方案。

https://github.com/pyenv/pyenv

Simple Python version management

 

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.

Terminal output example

 

 

功能

     对于每一个用户提供改变python全局版本的能力。

    对于每个项目的python版本提供支持。

pyenv does...

  • Let you change the global Python version on a per-user basis.
  • Provide support for per-project Python versions.
  • Allow you to override the Python version with an environment variable.
  • Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.

 

安装

自动安装

The automatic installer

Visit my other project: https://github.com/pyenv/pyenv-installer

Prerequisites

In general, compiling your own Python interpreter requires the installation of the appropriate libraries and packages. The installation wiki provides a list of these for common operating systems.

 

Install:

$ curl https://pyenv.run | bash

pyenv.run redirects to the install script in this repository and the invocation above is equivalent to:

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

Restart your shell so the path changes take effect:

$ exec $SHELL

You can now begin using pyenv.

Update:
$ pyenv update

 

 

按步骤安装(for Ubuntu)

如果自动安装失败,使用此方法安装。

https://github.com/pyenv/pyenv#installation

Check out pyenv where you want it installed. A good place to choose is $HOME/.pyenv (but you can install it somewhere else).

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

 

Define environment variable PYENV_ROOT to point to the path where pyenv repo is cloned and add $PYENV_ROOT/bin to your $PATH for access to the pyenv command-line utility.

  • For Ubuntu Desktop:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

     

Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc

 

Restart your shell so the path changes take effect. You can now begin using pyenv.

exec "$SHELL"

 

因为pyenv采取的方案是下载python包,然后进行编译安装的方法,所以需要先安装编译工具

Install Python build dependencies before attempting to install a new Python version.

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

 

Install Python versions into $(pyenv root)/versions. For example, to download and install Python 2.7.8, run:

pyenv install 2.7.8

 

说明

(1) 如果安装过程中遇到如下报错,

  File "/usr/bin/lsb_release", line 64
    print("No LSB modules are available.", file=sys.stderr)
                                               ^ SyntaxError: invalid syntax 

   则执行如下命令,可解决:

sudo rm -rf /usr/bin/lsb_release

 

from:

https://askubuntu.com/questions/853377/error-with-lsb-release-a-in-ubuntu-16-04-xenial

 

命令

https://github.com/pyenv/pyenv/blob/master/COMMANDS.md

完整命令

从中可以看到,创建虚拟环境的命令也在其中。

root:~/win10/mine# pyenv commands
--version
activate
commands
completions
deactivate
doctor
exec
global
help
hooks
init
install
installer
local
offline-installer
prefix
rehash
root
shell
shims
uninstall
update
version
version-file
version-file-read
version-file-write
version-name
version-origin
versions
virtualenv
virtualenv-delete
virtualenv-init
virtualenv-prefix
virtualenvs
whence
which
root:~/win10/mine#

 

pyenv环境覆盖关系

  三种环境, 按照优先级从高到低 shell > local > global

<pre>
+----------------------------------+
|                                  |
|   shell-specific Python version  |
|                                  |
+---------------+------------------+
                |
                v
+---------------+------------------+
|                                  |
|                                  |
|    local application-specific    |
|    Python Version                |
+---------------+------------------+
                |
                v
+---------------+------------------+
|                                  |
|                                  |
|      global version of Python    |
|                                  |
+----------------------------------+

</pre>

 

 

虚拟环境

创建2.7.13虚拟环境

  pyenv virtualenv 2.7.13 env2713

激活虚拟环境

  pyenv activate env271

如果要切换回系统环境, 运行这个命令即可

  pyenv deactivate

 

 

参考:

https://www.cnblogs.com/chjbbs/p/6611452.html

https://www.jianshu.com/p/158d880b2d41

posted @ 2020-08-27 15:23  lightsong  阅读(439)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel