一、使用 requirements.txt
1. 生成 requirements.txt
# 导出当前环境的所有包pip freeze > requirements.txt# 导出特定包pip freeze | grep package_name > requirements.txt# 使用 pipreqs 只导出项目实际使用的包pip install pipreqspipreqs /path/to/project
2. 从 requirements.txt 安装
# 从文件安装所有依赖pip install -r requirements.txt# 不使用缓存安装pip install -r requirements.txt --no-cache-dir# 从其他源的 requirements.txt 安装pip install -r https://example.com/requirements.txt
3. requirements.txt 格式示例
# requirements.txt 示例requests==2.28.1 # 精确版本numpy>=1.21.0,<2.0.0 # 版本范围pandas # 最新版本django~=3.2 # 兼容版本git+https://github.com/user/repo.git@branch # Git 仓库
二、配置 pip
1. 更换国内镜像源(加速下载)
# 临时使用国内镜像pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name# 设置默认镜像源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple# 常用国内镜像源# 清华:https://pypi.tuna.tsinghua.edu.cn/simple# 阿里云:https://mirrors.aliyun.com/pypi/simple/# 豆瓣:https://pypi.douban.com/simple/# 中科大:https://pypi.mirrors.ustc.edu.cn/simple/
2. 配置文件位置
# 查看 pip 配置pip config list# 编辑配置文件# Linux/Mac: ~/.pip/pip.conf# Windows: %APPDATA%\pip\pip.ini# 配置文件示例[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simpletrusted-host = pypi.tuna.tsinghua.edu.cn[install]use-mirrors = true
三、高级用法
1. 离线安装
# 下载包到本地(不安装)pip download package_name -d /path/to/dir# 示例:下载 requests 及其依赖pip download requests -d ./downloads# 从本地目录安装pip install --no-index --find-links=./downloads package_name# 离线安装所有依赖pip download -r requirements.txt -d ./offline_packagespip install --no-index --find-links=./offline_packages -r requirements.txt
2. 虚拟环境中使用 pip
# 创建虚拟环境python -m venv myenv# 激活虚拟环境# Windowsmyenv\Scripts\activate# Linux/Macsource myenv/bin/activate# 在虚拟环境中安装包pip install package_name# 退出虚拟环境deactivate
3. 使用缓存
# 查看缓存目录pip cache info# 清除缓存pip cache purge# 不使用缓存安装pip install --no-cache-dir package_name# 使用自定义缓存目录pip install --cache-dir /custom/cache/dir package_name
四、常见问题解决
1. 权限问题
# 用户安装(不推荐)pip install --user package_name# 使用 sudo(Linux/Mac)sudo pip install package_name# 使用虚拟环境(推荐)python -m venv myenvsource myenv/bin/activatepip install package_name
2. 网络问题
# 使用国内镜像源pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name# 设置代理pip install --proxy http://user:password@proxy.server:port package_name# 增加超时时间pip install --timeout 60 package_name
3. 版本冲突
# 查看冲突信息pip install package_name --verbose# 使用依赖解析器pip install --use-deprecated=legacy-resolver package_name# 升级 pip 到最新版本pip install --upgrade pip
五、实用示例
1. 批量安装常用库
# 创建 requirements.txtcat > requirements.txt << EOFnumpypandasmatplotlibseabornscikit-learnjupyterrequestsbeautifulsoup4lxmlpytestblackflake8EOF# 批量安装pip install -r requirements.txt
2. 数据科学环境配置
# 安装数据科学常用库pip install numpy scipy pandas matplotlib seaborn scikit-learn# 安装深度学习框架pip install tensorflow# 或pip install torch torchvision# 安装 Jupyter 环境pip install jupyter notebook ipython
📋 pip 常用命令速查表
| | |
|---|
install | | pip install requests |
uninstall | | pip uninstall requests |
list | | pip list |
show | | pip show requests |
freeze | | pip freeze > requirements.txt |
download | | pip download requests |
check | | pip check |
config | | pip config list |
cache | | pip cache purge |
六、pip 最佳实践总结
1. 使用虚拟环境
# 为每个项目创建独立的虚拟环境python -m venv project_envsource project_env/bin/activate # Linux/Macproject_env\Scripts\activate # Windows# 安装依赖pip install -r requirements.txt# 退出环境deactivate
2. 锁定依赖版本
# 生成精确的依赖文件pip freeze > requirements-lock.txt
3. 分层依赖管理
# requirements.txt-e .# requirements-dev.txt-r requirements.txtpytestblackflake8
4. 定期更新
# 查看过时的包pip list --outdated# 更新 pip 本身pip install --upgrade pip# 更新特定包pip install --upgrade package_name
✅ 进阶篇 学习检查清单
能创建和使用 requirements.txt
知道如何更换国内镜像源加速下载
掌握离线安装的方法
会使用虚拟环境管理项目依赖
能解决常见的安装问题
熟悉 pip 的高级命令
了解 pip 的最佳实践
(进阶篇):适合有一定经验的开发者,重点掌握依赖管理、镜像配置、虚拟环境等高级技巧。