[Python笔记] pyenv环境部署

@lianst  March 30, 2018

pyenv是一款Python多版本管理工具,它可以很好的管理Python解释器、Python版本和Python虚拟环境,对于日常开发中,是一款很有必要安装的利器。

安装依赖包

[python@vultr data]$ yum -y install gcc git make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel 

安装pyenv

[python@vultr data]$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.

WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by adding
# the following to ~/.bash_profile:

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

配置pyenv环境变量

[python@vultr data]$ vim ~/.bash_profile  //环境变量配置,追加到 ~/.bash_profile 文件中
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

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

[python@vultr data]$ source  ~/.bash_profile //source重载一下

pyenv常用操作


[python@VM_0_6_centos projects]$ pyenv --help
pyenv 1.2.3
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: https://github.com/pyenv/pyenv#readme

[python@VM_0_6_centos ~]$ pyenv versions  //查看当前可用Python版本
* system (set by /home/python/.pyenv/version) //系统版本

[python@VM_0_6_centos projects]$ pyenv version //查看当前Python版本
system (set by /home/python/.pyenv/version)

安装python版本的时候下载的安装包在/home/python/.pyenv/cache,也可以直接把Python的安装包下载下来之后放到这个路径里面,这样就可以实现离线安装了,需要注意的是Pyenv在安装的时候会随心所欲的选择安装版的后缀,比如有时候用*.tar.gz有时候用*.gz,所以建议把所有包都下载下来放到这个目录下面,让他自己选择。另外pyenv安装的python版本在/home/python/.pyenv/versions目录下面可以看到。


[python@VM_0_6_centos projects]$ pyenv install -l //列出当前可安装的版本
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5
....


[python@VM_0_6_centos magedu]$ pyenv install 3.5.3 //安装3.5.3版本
Downloading Python-3.5.3.tar.xz...
-> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
Installing Python-3.5.3...
Installed Python-3.5.3 to /home/python/.pyenv/versions/3.5.3

[python@VM_0_6_centos magedu]$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.5.3

创建虚拟环境

有时候我们在做项目的时候,会存在A项目用3.5.3版本环境,B项目使用2.7版本环境而且每个项目的三方库、扩展包都不一样,而且到项目做完打包的时候会把和本项目无关的三方库、扩展包都打进去。在这种情况下,我们可以使用pyenv来为每一个项目做一个虚拟环境,类似于VMWare虚拟机的概念,每一个项目就是一个虚拟环境(虚拟机),创建虚拟环境的操作我理解为是VMWare克隆虚拟机的操作。


[python@VM_0_6_centos cmdb]$ pyenv virtualenv 3.5.3 vir353 //基于3.5.3版本创建一个名为vir353的虚拟环境
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/vir353/lib/python3.5/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/vir353/lib/python3.5/site-packages

# 在可用版本目录里面已经可以看到我们创建的虚拟环境(虚拟版本)
[python@VM_0_6_centos projects]$ ls /home/python/.pyenv/versions/ 
3.5.3  vir353

然后我们进入到一个cmdb目录,然后切换到虚拟环境中。


[python@VM_0_6_centos cmdb]$ pyenv local vir353
(vir353) [python@VM_0_6_centos cmdb]$ 
(vir353) [python@VM_0_6_centos cmdb]$ pyenv version
vir353 (set by /home/python/test/projects/cmdb/.python-version)
[python@VM_0_6_centos projects]$ pyenv version
system (set by /home/python/.pyenv/version)

完成之后,你会发现在最左侧出现了我们创建的虚拟环境的版本,这样就说明当前目录是处于在一个虚拟环境中,返回到上一级目录就跳出了这个虚拟环境。


添加新评论