pyenv 介绍及部署记录(4)

为了对不同的项目进行隔离,使每个项目使用独立的python解释器及依赖,需要配置python虚拟环境.
每个项目都有一个单独的python虚拟环境, 这样项目之前的python环境相互隔离, 便于使用和管理!
 
[root@localhost ~]# pyenv versions   
  system
  2.7.5
* 3.6.1 (set by /root/.pyenv/version)
 
上面使用pyenv install安装的python版本,比如3.6.1
python3.6.1解释器安装的路径为~/.pyenv/versions/3.6.1/;
插件的安装的路径为~/.pyenv/versions/3.6.1/lib/python3.6/site-packages
 
[root@localhost ~]# ll -d /root/.pyenv/versions/3.6.1/
drwxr-xr-x 6 root root 4096 Dec 17 12:57 /root/.pyenv/versions/3.6.1/
[root@localhost ~]# ll -d /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages
drwxr-xr-x 8 root root 4096 Dec 17 12:57 /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages
 
使用pyenv-virtualenv创建python虚拟环境,实质上是在~/.pyenv/versions/3.6.1/下创建一个文件夹evns,存放该虚拟环境python的解释器;
并且在~/.pyenv/下创建一个软连接,该虚拟环境可以通过pyenv进行管理;
 
1) 比如创建某个项目的python虚拟环境, 虚拟环境的命令为kevin_py (名称随便起), 该虚拟环境的python版本是2.7.5
[root@localhost ~]# pyenv virtualenv 2.7.5 kevin_py
 
查看, 发现在~/.pyenv/versions目录下会有一个kevin_py虚拟环境的软连接
[root@localhost ~]# ll ~/.pyenv/versions/
total 8
drwxr-xr-x 7 root root 4096 Dec 17 14:53 2.7.5
drwxr-xr-x 6 root root 4096 Dec 17 12:57 3.6.1
lrwxrwxrwx 1 root root  41 Dec 17 14:53 kevin_py -> /root/.pyenv/versions/2.7.5/envs/kevin_py
 
查看python虚拟环境
[root@localhost ~]# pyenv virtualenvs
  2.7.5/envs/kevin_py (created from /root/.pyenv/versions/2.7.5)
  kevin_py (created from /root/.pyenv/versions/2.7.5)
 
有四种方法用于切换到python虚拟环境
 
1) 方法一  (推荐这一个切换方法)
[root@localhost ~]# pyenv activate kevin_py
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(kevin_py) [root@localhost ~]#
 
2) 方法二  (这种切换方法也推荐)
[root@localhost ~]# source activate kevin_py
pyenv-virtualenv: activate kevin_py
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(kevin_py) [root@localhost ~]# python -V
Python 2.7.5
(kevin_py) [root@localhost ~]#
 
3) 方法三
[root@localhost ~]# source /root/.pyenv/versions/2.7.5/envs/kevin_py/bin/activate kevin_py
(kevin_py) [root@localhost ~]# python -V
Python 2.7.5
(kevin_py) [root@localhost ~]#
 
4) 方法三
[root@localhost ~]# pyenv shell kevin_py
(kevin_py) [root@localhost ~]# python -V
Python 2.7.5
(kevin_py) [root@localhost ~]#
 
使用"source deactivate" 命令 或者 "pyenv deactivate"命令 退出python虚拟环境!!!!!!
退出后, 在python虚拟环境里部署的应用程序不受影响!
(kevin_py) [root@localhost ~]# source deactivate
pyenv-virtualenv: deactivate 2.7.5/envs/kevin_py
[root@localhost ~]#
 
[root@localhost ~]# pyenv activate kevin_py
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(kevin_py) [root@localhost ~]# pyenv deactivate
[root@localhost ~]#
 
 
2) 再创建另一个项目的python虚拟环境, 虚拟环境的命令为bobo_py, 该虚拟环境下的python版本为3.6.1
[root@localhost ~]# pyenv virtualenv 3.6.1 bobo_py
 
[root@localhost ~]# ll ~/.pyenv/versions/
total 8
drwxr-xr-x 7 root root 4096 Dec 17 14:53 2.7.5
drwxr-xr-x 7 root root 4096 Dec 17 15:00 3.6.1
lrwxrwxrwx 1 root root  40 Dec 17 15:00 bobo_py -> /root/.pyenv/versions/3.6.1/envs/bobo_py
lrwxrwxrwx 1 root root  41 Dec 17 14:53 kevin_py -> /root/.pyenv/versions/2.7.5/envs/kevin_py
 
[root@localhost ~]# pyenv virtualenvs
  2.7.5/envs/kevin_py (created from /root/.pyenv/versions/2.7.5)
  3.6.1/envs/bobo_py (created from /root/.pyenv/versions/3.6.1)
  bobo_py (created from /root/.pyenv/versions/3.6.1)
  kevin_py (created from /root/.pyenv/versions/2.7.5)
 
先切换到bobo_py虚拟环境
[root@localhost ~]# pyenv activate bobo_py
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(bobo_py) [root@localhost ~]# python -V
Python 3.6.1
(bobo_py) [root@localhost ~]#
 
然后再从bobo_py环境环境切换到另一个虚拟环境kevin_py
(bobo_py) [root@localhost ~]# source activate kevin_py
pyenv-virtualenv: activate kevin_py
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
 
退出虚拟环境
(kevin_py) [root@localhost ~]# source deactivate
pyenv-virtualenv: deactivate 2.7.5/envs/kevin_py
[root@localhost ~]#
 
注意: 上面介绍的四种虚拟环境的切换方法, 强烈建议采用第一种和第二种切换方法!
因为采用前两种切换方法进去后, 使用"source deactivate" 或者 "pyenv deactivate"命令可以正常退出来!
如果采用后两种切换方法进去后, 使用"source deactivate" 或者 "pyenv deactivate"命令退不出来!
 
使用"pyenv virtualenv-delete"命令 删除虚拟环境
[root@localhost ~]# pyenv virtualenvs
  2.7.5/envs/kevin_py (created from /root/.pyenv/versions/2.7.5)
  3.6.1/envs/bobo_py (created from /root/.pyenv/versions/3.6.1)
  bobo_py (created from /root/.pyenv/versions/3.6.1)
  kevin_py (created from /root/.pyenv/versions/2.7.5)
 
[root@localhost ~]# pyenv virtualenv-delete bobo_py
pyenv-virtualenv: remove /root/.pyenv/versions/3.6.1/envs/bobo_py? y
 
[root@localhost ~]# pyenv virtualenvs             
  2.7.5/envs/kevin_py (created from /root/.pyenv/versions/2.7.5)
  kevin_py (created from /root/.pyenv/versions/2.7.5)

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/12279.html