1. 安装
pip3 install virtualenv
2.环境使用
1,进入项目目录
[root@iZuf6fy2kg5mx828krkhcuZ test_obj]# ls
log.log manage.py __pycache__ README.md requirements.txt static tasks templates test_obj test_one
2.指定解释器创建虚拟环境
注:查看解释器位置
>>> import sys
>>> print(sys.executable)
[root@iZuf6fy2kg5mx828krkhcuZ test_obj]# virtualenv -p /usr/local/python3/bin/python3 venv_py3
Running virtualenv with interpreter /usr/local/python3/bin/python3
Already using interpreter /usr/local/python3/bin/python3
Using base prefix '/usr/local/python3'
New python executable in /object/test_obj/venv_py3/bin/python3
Also creating executable in /object/test_obj/venv_py3/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.
3.激活虚拟环境
[root@iZuf6fy2kg5mx828krkhcuZ test_obj]# source venv_py3/bin/activate
(venv_py3) [root@iZuf6fy2kg5mx828krkhcuZ test_obj]#
4.查看虚拟环境中的包
(venv_py3) [root@iZuf6fy2kg5mx828krkhcuZ test_obj]# pip3 list
Package Version
---------- -------
pip 19.3.1
setuptools 42.0.2
wheel 0.33.6
5.下载项目所需要的包
注:pip升级:pip install --upgrade pip
(venv_py3) [root@iZuf6fy2kg5mx828krkhcuZ test_obj]# sudo pip3 install -r requirements.txt
6.退出当前虚拟环境
(venv_py3) [root@iZuf6fy2kg5mx828krkhcuZ test_obj]# deactivate
[root@iZuf6fy2kg5mx828krkhcuZ test_obj]#
3. virtualenvwrapper
pip install virtualenvwrapper
### virtualenv和virtualenvwrapper配置
* 设置virtualenvwrapper,(注意python版本)在`~/.bashrc`文件末尾添加以下命令:
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 (默认使用的python3.7版本) #设置python版本和工作目录
export WORKON_HOME=/root/venv_list (工作目录,如果没有默认为~/.virutalenvs)
source /usr/bin/virtualenvwrapper.sh # 创建virtualenvwrap的虚拟工作环境
export PIP_DOWNLOAD_CACHE=$HOME/.pip-downloads # (可选)创建PIP下载软件包的缓存位置
* 为项目q_test设置虚拟环境
指定python解释器
mkvirtualenv --python=/usr/bin/python3.7 teq_testst_one
mkvirtualenv test_one
workon test_one
* 若后面要退出虚拟环境,可输入命令:
deactivate
* 删除
rmvirtualenv