安装 virtualenv
[root@bogon j]#
[root@bogon j]# python3 -m pip install virtualenv -i https://mirrors.aliyun.com/pypi/simple/
...
安装 virtualenv 管理工具 virtualenvwrapper
[root@bogon j]#
[root@bogon j]#
[root@bogon j]#
[root@bogon j]# python3 -m pip install virtualenvwrapper -i https://mirrors.aliyun.com/pypi/simple/
...
[root@bogon j]#
[root@bogon j]#
## 创建虚拟环境文件夹
[root@bogon j]# mkdir .virtualenvs
[root@bogon j]#
[root@bogon j]#
## 查看安装位置
[root@bogon j]#
[root@bogon j]# whereis virtualenv
virtualenv: /usr/local/bin/virtualenv
[root@bogon j]#
[root@bogon j]#
## 修改配置文件 添加后面带注释的4行
[root@bogon j]# vim ~/.bashrc
[root@bogon j]#
[root@bogon j]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export WORKON_HOME=/home/j/.virtualenvs # 虚拟环境文件位置
source /usr/local/bin/virtualenvwrapper.sh # 虚拟环境管理工具位置
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv # 虚拟环境安装位置
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # Python3 解释器位置
[root@bogon j]#
[root@bogon j]#
[root@bogon j]#
## 重新加载源文件
[root@bogon j]#
[root@bogon j]# source ~/.bashrc
/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/bin/python and that PATH is
set properly.
[root@bogon j]#
上面报错是因为,本地有两个Python环境
Python代表Python2
Python3才是Python3
下面修改一下这里
把python改为python3
## 查看管理工具 配置文件路径
[root@bogon j]# whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/local/bin/virtualenvwrapper.sh
[root@bogon j]#
[root@bogon j]#
[root@bogon j]#
## 把python改为python3
[root@bogon j]#
[root@bogon j]# vim /usr/local/bin/virtualenvwrapper.sh
[root@bogon j]#
[root@bogon j]#
## 重新加载源文件
[root@bogon j]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/get_env_details
[root@bogon j]#
[root@bogon j]#
常用命令
##创建虚拟环境
[root@bogon j]# mkvirtualenv test
created virtual environment CPython3.6.8.final.0-64 in 378ms
creator CPython3Posix(dest=/home/j/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/get_env_details
(test) [root@bogon j]#
(test) [root@bogon j]#
(test) [root@bogon j]#
(test) [root@bogon j]#
(test) [root@bogon j]#
## 退出虚拟环境
(test) [root@bogon j]# deactivate
[root@bogon j]#
[root@bogon j]#
## 列出虚拟环境
[root@bogon j]# workon
test
[root@bogon j]#
## 进入虚拟环境
[root@bogon j]# workon test
(test) [root@bogon j]#
(test) [root@bogon j]#
## 列出虚拟环境
(test) [root@bogon j]# lsvirtualenv
test
====
(test) [root@bogon j]#
(test) [root@bogon j]#
(test) [root@bogon j]#
(test) [root@bogon j]# deactivate
[root@bogon j]#
[root@bogon j]#
[root@bogon j]#
## 删除虚拟环境
[root@bogon j]# rmvirtualenv test
Removing test...
[root@bogon j]#
[root@bogon j]#
[root@bogon j]# lsvirtualenv
[root@bogon j]#
[root@bogon j]# workon
[root@bogon j]#
[root@bogon j]#
[root@bogon j]#