Python虚拟环境之pyenv-virtualenv插件


按照<<Python多版本环境管理之pyenv>>的操作安装pyenv后.我们可以方便的在不同版本中切换了.如果只开发一个项目的是没问题,但是如果同时进行多个项目,不同项目又依懒同一个包的不同版本的话就比较尴尬了.所以还需要基于同样的版本做不同的环境.
Linux下安装完pyenv后会附带安装pyenv-virtualenv,Mac系统需要自己手动安装一下

brew install pyenv-virtualenv

安装后确认:

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

准备就绪,开工
1.创建虚拟环境

先确认一下本机的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)

不指定版本,默认会使用当前环境版本.
创建时建议使用工程名+Python版本

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并查看

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

好了,我们现在有两个项目的同一个版本了

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


相关博文

About rainbird

IOS攻城狮
This entry was posted in linux, Mac, Python and tagged , , , , , , , , . Bookmark the permalink.

One Response to Python虚拟环境之pyenv-virtualenv插件

发表评论