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