按照<< Python多版本環境管理之pyenv >>的操做安裝pyenv後.咱們能夠方便的在不一樣版本中切換了.若是隻開發一個項目的是沒問題,可是若是同時進行多個項目,不一樣項目又依懶同一個包的不一樣版本的話就比較尷尬了.因此還須要基於一樣的版本作不一樣的環境.php
Linux下安裝完pyenv後會附帶安裝pyenv-virtualenv,Mac系統須要本身手動安裝一下python
brew install pyenv-virtualenv
安裝後確認:ide
Linux: ls ~/.pyenv/plugins pyenv-doctor pyenv-installer pyenv-update pyenv-virtualenv pyenv-which-ext python-build Mac: which pyenv-virtualenv /usr/local/bin/pyenv-virtualenv
準備就緒,開工ui
1.建立虛擬環境code
先確認一下本機的python版本 pyenv versions * system (set by /root/.pyenv/version) 3.7.3 3.8.2 手動指定3.7.3版本 後面跟的虛擬環境的名字 pyenv virtualenv 3.7.3 project-a-3.7.3 Looking in links: /tmp/tmpuhxuv288 Requirement already satisfied: setuptools in /root/.pyenv/versions/3.7.3/envs/project-a-3.7.3/lib/python3.7/site-packages (40.8.0) Requirement already satisfied: pip in /root/.pyenv/versions/3.7.3/envs/project-a-3.7.3/lib/python3.7/site-packages (19.0.3)
不指定版本,默認會使用當前環境版本.blog
建立時建議使用工程名+Python版本ip
2.查看虛擬環境開發
pyenv virtualenvs 3.7.3/envs/project-a-3.7.3 (created from /root/.pyenv/versions/3.7.3) project-a-3.7.3 (created from /root/.pyenv/versions/3.7.3)
再建立一個project-b並查看rem
pyenv virtualenv 3.7.3 project-b-3.7.3 Looking in links: /tmp/tmpa9ctl5a1 Requirement already satisfied: setuptools in /root/.pyenv/versions/3.7.3/envs/project-b-3.7.3/lib/python3.7/site-packages (40.8.0) Requirement already satisfied: pip in /root/.pyenv/versions/3.7.3/envs/project-b-3.7.3/lib/python3.7/site-packages (19.0.3) pyenv virtualenvs 3.7.3/envs/project-a-3.7.3 (created from /root/.pyenv/versions/3.7.3) 3.7.3/envs/project-b-3.7.3 (created from /root/.pyenv/versions/3.7.3) project-a-3.7.3 (created from /root/.pyenv/versions/3.7.3) project-b-3.7.3 (created from /root/.pyenv/versions/3.7.3
好了,咱們如今有兩個項目的同一個版本了get
3.激活虛擬環境
pyenv activate project-a-3.7.3 pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior. 驗證當前版本 python -V Python 3.7.3
4.退出虛擬環境
pyenv deactivate 再次驗證版本 python -V Python 2.7.17
發現版本已切回系統版本
5.刪除虛擬環境
pyenv uninstall project-a-3.7.3 pyenv-virtualenv: remove /root/.pyenv/versions/3.7.3/envs/project-a-3.7.3? y 再次查看虛擬環境發現只有project-b了 pyenv virtualenvs 3.7.3/envs/project-b-3.7.3 (created from /root/.pyenv/versions/3.7.3) project-b-3.7.3 (created from /root/.pyenv/versions/3.7.3)
that's all