centos7安装虚拟环境 virtualenvwrapper

1、个人安装环境

1.1 CentOS Linux release 7.7.1908 (Core)

2、安装步骤

2.1 查找python3命令路径

[root@localhost ~]# find / -name python3
/root/envs/py3test/bin/python3
/root/envs/p3test/bin/python3
/usr/bin/python3
/home/admin/Envs/py3test/bin/python3

 路径为:/usr/bin/python3

2.2 查找virtualenvwrapper.sh脚本的路径

[root@localhost ~]# whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/local/bin/virtualenvwrapper.sh

2.3 安装virtualenvwrapper

yum install python-setuptools python-devel
pip3 install virtualenvwrapper

2.4 编辑bashrc

[root@localhost ~]# vim .bashrc

 添加如下内容:

WORKON_HOME=~/Envs
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

解释如下:

  • WORKON_HOME为设置virtualenv的统一管理目录
  • VIRTUALENVWRAPPER_PYTHON指定python解释器的本体;

    需要特别注意,CentOS中默认安装python2,如果这边不指定,创建虚拟环境时将提示/usr/bin/python: No module named virtualenvwrapper

  • source执行virtualenvwrapper安装脚本

2.5 应用如上修改

source ~/.bashrc

3、虚拟环境使用

3.1 新建虚拟环境

3.1.1 新建python2环境

[root@localhost ~]# mkvirtualenv -p /usr/bin/python p2test

3.1.2 新建python3环境

[root@localhost ~]# mkvirtualenv p3test

 3.2 查看所有的虚拟环境

[root@localhost ~]# workon

运行结果:

p2test
p3test

 3.3 进入指定的虚拟环境

[root@localhost ~]# workon p2test

 

(p2test) [root@localhost ~]# python

 运行结果:

Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

3.4 退出虚拟环境

(p2test) [root@localhost ~]# deactivate

3.5 删除虚拟环境

[root@localhost ~]# rmvirtualenv py3test

 

posted @ 2020-02-26 22:22  “人生苦短”  阅读(1936)  评论(0编辑  收藏  举报