python-learning02 - pyenv和virtualenv教程

1. 安裝pyenv

pyenv 用來管理 Python 的版本,Python 2.x 和 Python 3.x版本之間靈活切換,virtualenv隔離每個項目包,結合pyenv管理Python的版本,不同項目需要依賴的包版本不同,則需要使用虛擬環境。

1.1 部署pyenv環境

[root@localhost ~]# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/root/.pyenv'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 17361 (delta 1), reused 5 (delta 1), pack-reused 17352
Receiving objects: 100% (17361/17361), 3.38 MiB | 431.00 KiB/s, done.
Resolving deltas: 100% (11819/11819), done.

1.2 配合環境變量

寫入bash_profile

### pyenv env PATH
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile

echo  'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile 

echo  'eval "$(pyenv init -)"' >> ~/.bash_profile

source ~/.bash_profile 

2. 命令使用

Usage: pyenv []

commands 含義
commands 顯示所有的command
local 顯示本地的python版本
global 設置或者顯示python版本
shell 設置或者顯示shell目前使用的python版本
install 安裝python版本
uninstall U卸載指定python版本
rehash Rehash pyenv shims (run this after installing executables)
version 顯示當前的python版本
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

2.1 安裝python版本

[root@localhost ~]# pyenv install 2.7.14
Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
Installing Python-2.7.14...
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python zlib extension was not compiled. Missing the zlib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (Red Hat Enterprise Linux Server 7.4 using python-build 1.2.13-2-g0aeeb6f)

Inspect or clean up the working tree at /tmp/python-build.20190823124027.2535
Results logged to /tmp/python-build.20190823124027.2535.log

Last 10 log lines:
rm -f /root/.pyenv/versions/2.7.14/share/man/man1/python.1
(cd /root/.pyenv/versions/2.7.14/share/man/man1; ln -s python2.1 python.1)
if test "xno" != "xno" ; then \
        case no in \
                upgrade) ensurepip="--upgrade" ;; \
                install|*) ensurepip="" ;; \
        esac; \
         ./python -E -m ensurepip \
                $ensurepip --root=/ ; \
fi

存在報錯:ERROR: The Python zlib extension was not compiled. Missing the zlib?
解決方案:https://github.com/pyenv/pyenv/wiki/common-build-problems

  • 根據解決方案,安裝相關的安裝包
## 安裝前,配置鏡像源,配置epel源
yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel findutils
[root@localhost ~]# pyenv install 2.7.14
Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz

Installing Python-2.7.14...
Installed Python-2.7.14 to /root/.pyenv/versions/2.7.14

python 3

[root@localhost ~]# pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
-> https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
Installing Python-3.5.4...
Installed Python-3.5.4 to /root/.pyenv/versions/3.5.4

2.2 查看當前python版本

2.3 設置版本

pyenv redhash --> 創建墊片路徑,爲已經安裝可執行文件創建shims,每次增刪python版本之後,都應該執行這次命令。

  • 設置當前shell默認的python版本
[root@localhost ~]# pyenv shell 2.7.14
[root@localhost ~]# python 
Python 2.7.14 (default, Aug 23 2019, 13:34:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • 設置本地版本local
pyenv local 2.7.14
pyenv rehash
  • 設置global全局版本
[root@localhost ~]# pyenv global 2.7.14
[root@localhost ~]# 
[root@localhost ~]# pyenv rehash 
[root@localhost ~]# pyenv versions
  system
* 2.7.14 (set by PYENV_VERSION environment variable)
  3.5.4

3. virtualenv管理項目

virtualenv是個獨立的項目,用來隔離不同的項目的工作環境,比如在同一臺主機上面,不同的項目需要使用不同版本的python

3.1 安裝pyenv-virtualenv

[root@localhost ~]# git clone https://github.com/yyuu/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 2064, done.
remote: Total 2064 (delta 0), reused 0 (delta 0), pack-reused 2064
Receiving objects: 100% (2064/2064), 580.31 KiB | 211.00 KiB/s, done.
Resolving deltas: 100% (1413/1413), done.
[root@localhost ~]# echo $(pyenv root)
/root/.pyenv
[root@localhost ~]# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
[root@localhost ~]# source ~/.bash_profile
[root@localhost ~]# pyenv help virtualenv 
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
       pyenv virtualenv --version
       pyenv virtualenv --help

  -f/--force Install even if the version appears to be installed already

3.2 pyenv-virtualenv使用

爲同一個python解釋器,創建不同的工作環境

  • 創建兩個項目,一個用python2,一個用python3
[root@localhost ~]# pyenv virtualenv 2.7.14 python2-learning
Collecting virtualenv
  Downloading https://mirrors.aliyun.com/pypi/packages/f7/69/1ad2d17560c4fc60170056dcd0a568b83f3453a2ac91155af746bcdb9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB)
    100% |████████████████████████████████| 3.3MB 323kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.4
You are using pip version 9.0.1, however version 19.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
New python executable in /root/.pyenv/versions/2.7.14/envs/python2-learning/bin/python2.7
Also creating executable in /root/.pyenv/versions/2.7.14/envs/python2-learning/bin/python
Installing setuptools, pip, wheel...
done.
Requirement already satisfied: setuptools in /root/.pyenv/versions/2.7.14/envs/python2-learning/lib/python2.7/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/2.7.14/envs/python2-learning/lib/python2.7/site-packages
[root@localhost ~]# pyenv virtualenv 3.5.4 python3-learning
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.5.4/envs/python3-learning/lib/python3.5/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/3.5.4/envs/python3-learning/lib/python3.5/site-packages
[root@localhost ~]# ls
anaconda-ks.cfg epel-release-latest-7.noarch.rpm python.1 RedHat-7.4-x86_64-dvd.iso requirements.txt zhanghao
[root@localhost ~]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
  • 查看創建之後的工程目錄

python2-learning (created from /root/.pyenv/versions/2.7.14)
python3-learning (created from /root/.pyenv/versions/3.5.4)

[root@localhost ~]# cd /root/.pyenv/versions/2.7.14
[root@localhost 2.7.14]# ls
bin envs include lib share
[root@localhost 2.7.14]# cd envs/
[root@localhost envs]# ls
python2-learning
[root@localhost envs]# cd python2-learning/
[root@localhost python2-learning]# ls
bin include lib
[root@localhost python2-learning]# 
  • 檢驗環境是否隔離
[root@localhost python2-learning]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost python2-learning]# pyenv activate python2-learning 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(python2-learning) [root@localhost python2-learning]# pip install flask
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting flask
  Downloading https://mirrors.aliyun.com/pypi/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
     |████████████████████████████████| 102kB 2.8MB/s 
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
     |████████████████████████████████| 337kB 3.4MB/s 
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
     |████████████████████████████████| 81kB 3.8MB/s 
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
     |████████████████████████████████| 133kB 3.8MB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
(python2-learning) [root@localhost python2-learning]# 
(python2-learning) [root@localhost python2-learning]# python
Python 2.7.14 (default, Aug 23 2019, 13:34:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@localhost ~]# pyenv virtualenvs 
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost ~]# pyenv activate python3-learning 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(python3-learning) [root@localhost ~]# python
Python 3.5.4 (default, Aug 23 2019, 14:12:04) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • 刪除項目環境
[root@localhost ~]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost ~]# pyenv virtualenv-delete python3-learning 
pyenv-virtualenv: remove /root/.pyenv/versions/3.5.4/envs/python3-learning? y
[root@localhost ~]# pyenv virtualenvs 
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
[root@localhost ~]# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章