venv 虛擬環境管理
venv 虛擬環境的優點
- 獨立的 Python 環境,不會產生衝突
- 有助於包的管理
- 刪除和卸載方便
venv 使用方法
- 創建虛擬環境
python3 -m venv test
- 激活虛擬環境
切換指定文件夾
Windows:/Scripts/
macOS:/bin/
執行指令:`activate`
venv 安裝 Python 包
-
Python 版本選擇
- 進入 python2.7 環境:
python2
- 進入 python3.x 環境:
python3
- 進入 python2.7 環境:
-
pip 安裝 Python 包
- 安裝 Python2.x 版本的包
- 安裝 Python3.x 版本的包
venv 退出和刪除
- 退出虛擬環境:deactivate
- 刪除虛擬環境:刪除環境目錄
pip環境管理
pip 概述
-
pip 是 Python 包管理工具
- python2 的 2.7.9 版本開始自帶
- python3 的 3.4 版本開始自帶
-
https://pypi.org/ 託管了大量流行的 Python 包
pip 常用命令
功能 | 指令 |
---|---|
查看 pip 版本 | pip -V |
查看幫助文檔 | pip help |
查看包列表 | pip list |
導出包列表 | pip freeze |
安裝 | pip install 包名 |
升級 | pip install -U 包名 |
卸載 | pip uninstall 包名 |
pip 安裝包
- 普通安裝
- 指定版本
- 從文件中安裝
# 默認安裝最新版本
$ pip install pytest
# 執行版本
$ pip install pytest==6.2.0
# 從文件清單中批量安裝
$ pip install -r requirments.txt
# 文件格式
pytest==6.2.0
Faker==9.3.1
selenium==3.14.1
pip 升級包
- 升級已安裝的 Python 包
$ pip install -U pytest
pip 卸載包
- 卸載 Python 包
# 卸載包
$ pip uninstall pytest
pip 使用鏡像加速
-
pip install -i 鏡像源
-
國內常用源
- 阿里源:Simple Index
- 清華源:Simple Index
- 豆瓣源:Simple Index
# 使用鏡像
pip install pytest -i https://pypi.douban.com/simple