windows环境
1、安装virtualenv
pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
2、创建虚拟环境
python -m venv venv_name
3、激活虚拟环境
C:> <venv>/Scripts/activate.bat
4、退出虚拟环境
../Scripts>deactivate.bat
Linux环境
1、安装pip
[root@iZm5e4p9y07nsbfvsrcsx1Z ~]# yum install python3-pip
2、安装virtualenv
[root@iZm5e4p9y07nsbfvsrcsx1Z ~]# pip install virtualenv
3、创建虚拟环境
[root@iZm5e4p9y07nsbfvsrcsx1Z ~]# mkdir /home/virtualenv
[root@iZm5e4p9y07nsbfvsrcsx1Z ~]# cd /home/virtualenv/
[root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]# virtualenv first_env
4、激活虚拟环境,source 虚拟环境目录/bin/activate
[root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]# source first_env/bin/activate
(first_env) [root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]#
(first_env) [root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]# pip install requests
Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting requests
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 1.5 MB/s
Collecting certifi>=2017.4.17
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
|████████████████████████████████| 147 kB 4.6 MB/s
Collecting chardet<5,>=3.0.2
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 53.2 MB/s
Collecting urllib3<1.27,>=1.21.1
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/23/fc/8a49991f7905261f9ca9df5aa9b58363c3c821ce3e7f671895442b7100f2/urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
|████████████████████████████████| 137 kB 77.5 MB/s
Collecting idna<3,>=2.5
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 43.8 MB/s
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.3
(first_env) [root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]# pip list
Package Version
---------- ---------
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 21.0.1
requests 2.25.1
setuptools 52.0.0
urllib3 1.26.3
wheel 0.36.2
(first_env) [root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]#
5、退出虚拟环境
(first_env) [root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]# deactivate
[root@iZm5e4p9y07nsbfvsrcsx1Z virtualenv]#