venv支持python3.8以及更好版本。用于单独管理不同项目的软件包安装
1.venv创建虚拟环境
python -m venv venv_name
在当前目录创建一个名为venv_name的虚拟环境
–without-pip: 不需要安装 pip,默认为安装
–clear:如果创建虚拟环境的目录已经有了其他虚拟环境,清除重建
2.激活
Windows 系统
<myvenv>\Scripts\activate.bat
powershell 命令行,activate.bat换成 Activate.ps1
Linux 系统中
source <myvenv>/bin/activate
将<myvenv>换为自己的虚拟环境目录
检查是否激活
where python
当虚拟环境处于激活状态,上述命令会输出一个包含<myvenv>目录,以<myvenv>\bin\python.exe结尾的文件路径
3.退出
deactivate
关闭shell后将停用虚拟环境,打开新的shell,需要重新激活
4.pip
更新到最新版本
python3 -m pip install --upgrade pip
安装特定软件包版本
python3 -m pip install 'requests==2.18.4'
要安装最新的2.x版本
python3 -m pip install ‘requests>=2.0.0,❤️.0.0’
使用其他源安装
python3 -m pip install -i http://index.example.com/simple/ SomeProject
允许使用其他源和PyPI同时使用
python3 -m pip install --extra-index-url http://index.example.com/simple/ SomeProject
国内镜像源
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣:http://pypi.douban.com/simple/
升级软件包
python3 -m pip install --upgrade requests
重建环境时,使用freeze生成原有环境的requirements.txt文件
python3 -m pip freeze
pip freeze > requirements.txt
使用requirements.txt文件批量安装
requirements.txt内容:
requests==2.18.4
google-auth==1.1.0
python3 -m pip install -r requirements.txt