Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • python 学习指南
  • python 中的设计模式
  • module

  • tool

    • python 虚拟环境
    • python 调试工具embed
    • python 调试工具pdb
    • python 包管理器 pip
      • 基础命令
        • 查看帮助和版本
        • 包管理命令
        • 安装包
      • 为指定 Python 版本安装模块
        • 方案一: 使用 python -m pip
        • 方案二: 使用 pyenv 切换版本
      • 版本控制
        • 版本符号说明
        • 版本控制示例
        • requirements.txt 版本控制
      • 查看包的可用版本
        • 方法 1: 使用 PyPI 网站
        • 方法 2: 使用 pip index versions
        • 方法 3: 尝试安装不存在的版本
        • 方法 4: 使用第三方工具
      • 配置 pip
        • 配置文件位置
        • 配置镜像源
        • 临时使用镜像源
        • 永久配置镜像源
        • 常用国内镜像源
      • 常见问题
        • 权限问题
        • 清理缓存
        • SSL 证书错误
        • 查看安装位置
      • 最佳实践
      • 参考链接
    • pyenv 好用的python版本管理工具
  • other

  • 《python》
  • tool
Jacky
2023-06-02
目录

python 包管理器 pip

pip 是 Python 的包管理器, 用于安装、升级和管理 Python 包。它是 Python 的官方包管理工具, 提供了便捷的方式来获取、安装和管理第三方库和工具

# 基础命令

# 查看帮助和版本

# 查看帮助
pip --help
pip install --help

# 查看版本
pip --version
pip -V
1
2
3
4
5
6
7

# 包管理命令

# 列出已安装的包
pip list

# 列出过期的包
pip list --outdated

# 显示包的详细信息
pip show requests

# 检查包的依赖关系
pip check

# 导出包列表
pip freeze > requirements.txt

# 卸载包
pip uninstall requests

# 卸载多个包
pip uninstall requests numpy pandas

# 卸载 requirements.txt 中的所有包
pip uninstall -r requirements.txt -y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 安装包

# 安装最新版本
pip install requests

# 安装指定版本
pip install requests==2.28.0

# 升级包
pip install --upgrade requests
pip install -U requests

# 从 requirements.txt 安装
pip install -r requirements.txt

# 安装本地包
pip install /path/to/package

# 以可编辑模式安装(开发模式)
pip install -e .
pip install -e /path/to/project

# 从 Git 仓库安装
pip install git+https://github.com/user/repo.git
pip install git+https://github.com/user/repo.git@branch_name

# 安装 wheel 文件
pip install package.whl

# 指定安装源
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 为指定 Python 版本安装模块

在系统安装了多个 python3 编译器的场景下, 直接使用 pip 安装到不同的编译器中, 类似于下面这样的输出

多个 Python 版本示例

# 方案一: 使用 python -m pip

# 使用指定的 Python 版本安装包
python3.9 -m pip install pytest
python3.10 -m pip install pytest

# 或使用环境变量
$PYTHON -m pip install pytest
1
2
3
4
5
6

# 方案二: 使用 pyenv 切换版本

如: 使用 python3.9 版本指定安装

  1. 使用 pyenv 切换 python 到 3.9 版本. pyenv 的使用可以查看 pyenv 文档
  2. python3 -m pip install pytest, 安装完后, 截图如下

Python 3.9 安装示例

# 版本控制

# 版本符号说明

在通过 pip 安装包时,可以使用以下版本控制符号:

符号 说明 示例 含义
== 精确版本 requests==2.28.0 必须是 2.28.0 版本
>= 大于等于 requests>=2.28.0 2.28.0 或更高版本
<= 小于等于 requests<=2.28.0 2.28.0 或更低版本
> 大于 requests>2.28.0 高于 2.28.0 的版本
< 小于 requests<2.28.0 低于 2.28.0 的版本
!= 不等于 requests!=2.28.0 除了 2.28.0 的任何版本
~= 兼容版本 requests~=2.28.0 >= 2.28.0 且 < 2.29.0
, 组合条件 requests>=2.25.0,<3.0.0 2.25.0 到 3.0.0 之间

# 版本控制示例

# 安装特定版本
pip install 'requests==2.28.0'

# 安装版本范围
pip install 'numpy>=1.18.0,<=1.19.0'

# 兼容版本(推荐用于主版本控制)
pip install 'pyarrow~=13.0.0'  # >= 13.0.0 且 < 14.0.0

# 排除特定版本
pip install 'django!=3.0.0'

# 组合多个条件
pip install 'pandas>=1.0.0,<2.0.0,!=1.5.0'
1
2
3
4
5
6
7
8
9
10
11
12
13
14

注意

使用引号将版本范围括起来,确保 < 等符号被正确解释为版本限制符号,而不是 shell 的输入重定向符号。

# requirements.txt 版本控制

# requirements.txt 示例
requests==2.28.0
numpy>=1.20.0,<2.0.0
pandas~=1.5.0
django>=3.2,<4.0
flask
1
2
3
4
5
6

# 查看包的可用版本

提示

pip search 命令已被 PyPI 废弃,请使用以下替代方法。

# 方法 1: 使用 PyPI 网站

访问 https://pypi.org/search (opens new window) 搜索包名,查看所有可用版本。

# 方法 2: 使用 pip index versions

# 查看包的所有可用版本
pip index versions requests

# 输出示例:
# requests (2.31.0)
# Available versions: 2.31.0, 2.30.0, 2.29.0, ...
1
2
3
4
5
6

# 方法 3: 尝试安装不存在的版本

# pip 会列出所有可用版本
pip install requests==

# 输出示例:
# ERROR: Could not find a version that satisfies the requirement requests==
# (from versions: 0.2.0, 0.2.1, ..., 2.31.0)
1
2
3
4
5
6

# 方法 4: 使用第三方工具

# 安装 yolk3k
pip install yolk3k

# 查看包的版本信息
yolk -V requests
1
2
3
4
5

# 配置 pip

# 配置文件位置

pip 配置文件的位置(按优先级排序):

  1. 命令行参数 - 最高优先级
  2. 环境变量 - PIP_* 形式
  3. 用户配置文件:
    • Linux/macOS: ~/.config/pip/pip.conf 或 ~/.pip/pip.conf
    • Windows: %APPDATA%\pip\pip.ini
  4. 全局配置文件:
    • Linux/macOS: /etc/pip.conf
    • Windows: C:\ProgramData\pip\pip.ini

# 配置镜像源

# 临时使用镜像源

# 使用清华源
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

# 使用阿里云源
pip install requests -i https://mirrors.aliyun.com/pypi/simple/

# 使用豆瓣源
pip install requests -i https://pypi.douban.com/simple/
1
2
3
4
5
6
7
8

# 永久配置镜像源

创建或编辑 ~/.config/pip/pip.conf (Linux/macOS) 或 %APPDATA%\pip\pip.ini (Windows):

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
1
2
3
4
5
6

或使用命令配置:

# 配置清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 查看配置
pip config list

# 查看配置文件位置
pip config debug
1
2
3
4
5
6
7
8

# 常用国内镜像源

镜像源 URL
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple (opens new window)
阿里云 https://mirrors.aliyun.com/pypi/simple/ (opens new window)
豆瓣 https://pypi.douban.com/simple/ (opens new window)
中科大 https://pypi.mirrors.ustc.edu.cn/simple/ (opens new window)
华为云 https://mirrors.huaweicloud.com/repository/pypi/simple/ (opens new window)

# 常见问题

# 权限问题

# 使用 --user 安装到用户目录
pip install --user requests

# 或使用虚拟环境(推荐)
python -m venv myenv
source myenv/bin/activate  # Linux/macOS
myenv\Scripts\activate     # Windows
pip install requests
1
2
3
4
5
6
7
8

# 清理缓存

# 清理所有缓存
pip cache purge

# 查看缓存信息
pip cache info

# 列出缓存的包
pip cache list
1
2
3
4
5
6
7
8

# SSL 证书错误

# 临时信任主机
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org requests

# 或禁用 SSL 验证(不推荐)
pip install --index-url=http://pypi.douban.com/simple/ --trusted-host pypi.douban.com requests
1
2
3
4
5

# 查看安装位置

# 查看包的安装位置
pip show requests

# 查看 Python 包的搜索路径
python -c "import sys; print('\n'.join(sys.path))"
1
2
3
4
5

# 最佳实践

  1. 使用虚拟环境 - 避免全局安装污染
  2. 固定版本 - 在 requirements.txt 中使用精确版本
  3. 定期更新 - 使用 pip list --outdated 检查过期包
  4. 使用国内镜像 - 提高下载速度
  5. 避免使用 sudo - 使用 --user 或虚拟环境

# 参考链接

  • pip 官方文档 (opens new window)
  • PyPI 官网 (opens new window)
  • Python 包管理最佳实践 (opens new window)
#Python#pip
上次更新: 2025/10/09, 22:06:28
python 调试工具pdb
pyenv 好用的python版本管理工具

← python 调试工具pdb pyenv 好用的python版本管理工具→

最近更新
01
npx 使用指南
10-12
02
cursor
09-28
03
inspect
07-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式