一、常见的python环境管理模块

1、virtualenv

#安装
hadoop000:test wn$ pip install virtualenv
#使用,创建当前环境默认的python版本环境
hadoop000:test wn$ virtualenv testenv
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.5'
New python executable in /Users/wn/test/envtest/bin/python3
Also creating executable in /Users/wn/test/envtest/bin/python
Installing setuptools, pip, wheel...
done.
#会在当前目录下生成一个testenv的目录
#进入虚拟环境
hadoop000:test wn$ source envtest/bin/activate
(envtest) hadoop000:test wn$ 
#查看当前环境下下的包,这是与系统隔离的,我们后续进行安装时,就会安装在这里
(envtest) hadoop000:test wn$ pip list
Package    Version
---------- -------
pip        19.0.1 
setuptools 40.7.1 
wheel      0.32.3 
#退出当前虚拟环境
(envtest) hadoop000:test wn$ deactivate
hadoop000:test wn$

2、virtualenvwrapper

#安装
hadoop000:test wn$ sudo easy_install virtualenvwrapper
#不知道为什么,我使用pip install virtualenvwrapper进行安装,结果发现没有找到/usr/local/bin/virtualenvwrapper.sh,使用easy_install安装后就出现了

#在环境变量中设置工作环境目录,并激活virtualenvwrapper
hadoop000:bin wn$ vim ~/.bash_profile
export WORKON_HOME=~/VirtualenvWrapper
source /usr/local/bin/virtualenvwrapper.sh
hadoop000:bin wn$ source ~/.bash_profile
#这时能在当前用户的根目录下找到VirtualenvWrapper目录

#创建虚拟环境,同时会直接激活创建的环境
hadoop000:~ wn$ mkvirtualenv testenv2
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.5'
New python executable in /Users/wn/VirtualenvWrapper/testenv2/bin/python3
Also creating executable in /Users/wn/VirtualenvWrapper/testenv2/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /Users/wn/VirtualenvWrapper/testenv2/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/wn/VirtualenvWrapper/testenv2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/wn/VirtualenvWrapper/testenv2/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/wn/VirtualenvWrapper/testenv2/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/wn/VirtualenvWrapper/testenv2/bin/get_env_details
(testenv2) hadoop000:~ wn$ 

#在我们刚创建的目录下能看到我们新建的虚拟环境
(testenv2) hadoop000:VirtualenvWrapper wn$ ll
total 0
drwxr-xr-x   3 wn  staff    96  1 30 10:55 ./
drwxr-xr-x+ 54 wn  staff  1728  1 30 10:52 ../
drwxr-xr-x   6 wn  staff   192  1 30 10:55 testenv2/

#退出当前虚拟环境
(testenv2) hadoop000:VirtualenvWrapper wn$ deactivate 
hadoop000:VirtualenvWrapper wn$ 

#查看虚拟环境列表
hadoop000:VirtualenvWrapper wn$ workon
testenv2

#进入虚拟环境
hadoop000:VirtualenvWrapper wn$ workon testenv2
(testenv2) hadoop000:VirtualenvWrapper wn$

二、python版本管理模块

通常情况下以上两种虚拟环境管理的工具已经足够使用,但是在python版本管理层面做的并不是太好

1、pyenv python版本管理模块

pyenv是通过修改系统环境变量实现版本管理的,而不是通过目录切换

1)安装、更新、卸载pyenv

github:https://github.com/pyenv/pyenv-installer

# 安装pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
# 安装完成后可以看到提示
WARNING: seems you still have not added 'pyenv' to the load path.

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

export PATH="/Users/wn/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# 由于我的系统中没有bashrc,所以pyenv的环境变量没有被设置

#设置环境变量
hadoop000:VirtualenvWrapper wn$ vim ~/.bashrc
export PATH="/Users/wn/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
hadoop000:~ wn$ source ~/.bashrc 

# 检查pyenv
hadoop000:~ wn$ pyenv
pyenv 1.2.9
Usage: pyenv <command> [<args>]

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 <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

# 具体的更新和删除,请自行到github上查看
2)pyenv使用
# 查看可安装的python版本
hadoop000:~ wn$ pyenv install -l

# 安装卸载python版本
hadoop000:~ wn$ pyenv install 2.7.15
hadoop000:~ wn$ pyenv uninstall 2.7.15
hadoop000:~ wn$ pyenv install 3.6.8

# 遇到问题,一个明显的问题zlib未安装:
ERROR: The Python zlib extension was not compiled. Missing the zlib?
根据官方的提示,进行操作发现并没有成功
https://github.com/pyenv/pyenv/wiki/common-build-problems
安装了:
brew install readline xz
问题依旧没有解决

决定自行安装依赖:
hadoop000:~ wn$ brew install zlib
配置环境变量:
hadoop000:~ wn$ vim ~/.bashrc 
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
hadoop000:~ wn$ source ~/.bashrc

#再次安装,成功
hadoop000:~ wn$ pyenv install 2.7.15
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-2.7.15.tar.xz...
-> https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tar.xz
Installing Python-2.7.15...
python-build: use readline from homebrew
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-2.7.15 to /Users/wn/.pyenv/versions/2.7.15

# 查看当前环境的pyenv环境列表,*表示当前正在使用的python版本
hadoop000:~ wn$ pyenv versions
* system (set by /Users/wn/.pyenv/version)
  2.7.15
  3.6.8
# 查看当前环境的python
hadoop000:~ wn$ python
Python 2.7.10 (default, Aug 17 2018, 19:45:58) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
# pyenv切换成2.7.15
hadoop000:~ wn$ pyenv shell 2.7.15
hadoop000:~ wn$ python
Python 2.7.15 (default, Jan 30 2019, 11:53:16) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
# pyenv切换成3.6.8
hadoop000:~ wn$ pyenv shell 3.6.8
hadoop000:~ wn$ python
Python 3.6.8 (default, Jan 30 2019, 11:58:36) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
3)pyenv与virtualenv的组合使用

查看pyenv插件

hadoop000:~ wn$ ls -la ~/.pyenv/plugins/
total 8
drwxr-xr-x   9 wn  staff  288  1 30 11:15 .
drwxr-xr-x  24 wn  staff  768  1 30 11:20 ..
-rw-r--r--   1 wn  staff   52  1 30 11:14 .gitignore
drwxr-xr-x  11 wn  staff  352  1 30 11:14 pyenv-doctor
drwxr-xr-x  10 wn  staff  320  1 30 11:14 pyenv-installer
drwxr-xr-x   9 wn  staff  288  1 30 11:15 pyenv-update
drwxr-xr-x  14 wn  staff  448  1 30 11:15 pyenv-virtualenv
drwxr-xr-x   8 wn  staff  256  1 30 11:15 pyenv-which-ext
drwxr-xr-x   8 wn  staff  256  1 30 11:14 python-build

pyenv virtualenv创建虚拟环境

hadoop000:~ wn$ pyenv virtualenv 2.7.15 envtest3
#激活envtest3
hadoop000:~ wn$ pyenv activate envtest3 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(envtest3) hadoop000:~ wn$ 
#退出envtest3
(envtest3) hadoop000:~ wn$ source deactivate
pyenv-virtualenv: deactivate 2.7.15/envs/envtest3
hadoop000:~ wn$

查看pyenv virtualenvs有哪些环境,以及其对应的python版本

(celeryenv) hadoop000:~ wn$ pyenv virtualenvs
  2.7.15/envs/envtest3 (created from /Users/wn/.pyenv/versions/2.7.15)
  3.6.8/envs/celeryenv (created from /Users/wn/.pyenv/versions/3.6.8)
* celeryenv (created from /Users/wn/.pyenv/versions/3.6.8)
  envtest3 (created from /Users/wn/.pyenv/versions/2.7.15)