admin管理员组

文章数量:1130349

pip(Python 的包安装器) 从概念到实战、再到安全与排错一次讲清。你可以把这当成一份随时可抄的“pip 作业手册”。

1) pip 是什么 & 正确调用方式

  • 作用:从索引(默认 PyPI)下载并安装wheel/sdist 包,解析依赖、卸载、查询等。

  • 推荐调用始终用你要用的解释器来调用——避免多 Python 冲突:

    python -m pip --version
    python -m pip install <pkg>
    

    pip/pip3 可能指向别的解释器;用 python -m pip 最稳。

2) 基本安装与虚拟环境(强烈建议)

# 创建隔离环境(Linux/macOS)
python -m venv .venv && source .venv/bin/activate
# Windows
python -m venv .venv && .\.venv\Scripts\activate

# 安装/升级
python -m pip install -U pip             # 升级 pip 自身
python -m pip install requests           # 安装
python -m pip uninstall requests         # 卸载
python -m pip show requests              # 查看详情
python -m pip list --outdated            # 查看可升级包
python -m pip check                      # 检查依赖是否冲突/缺失

不要在系统 Python 全局装包;必要时用 --user(装到“用户 site-packages”)或开启虚拟环境。

3) requirements / constraints / 锁定

  • requirements.txt:要安装的包清单(可写版本范围或固定版本)。

  • constraints 文件:只约束版本,不会被直接安装;与多个 requirements 共享约束非常有用。

    python -m pip install -r requirements.txt -c constraints.txt
    
  • 固定依赖(可复现构建)

    • 简单:pip freeze > requirements.txt(记录当前环境的全部精确版本)。

    • 更专业:pip-tools 生成锁定版本(含哈希):

      python -m pip install pip-tools
      pip-compile --generate-hashes       # 从 requirements.in 解析并锁定
      pip-sync                            # 按锁文件同步环境
      

4) 选择索引/镜像与私有仓

  • 临时切换:

    python -m pip install -i https://pypi/simple <pkg>
    python -m pip install --index-url https://<私有代理>/simple <pkg>
    python -m pip install --extra-index-url https://<次级源>/simple <pkg>
    

    安全建议:优先使用单一私有代理(代理上游 PyPI),减少“依赖混源”引发的 dependency confusion 风险;如必须混源,确保私有源在前,并对私有命名空间做白名单。

  • 永久配置(按 OS):

    # 查看/设置
    python -m pip config list
    python -m pip config set global.index-url https://<mirror>/simple
    python -m pip config set global.trusted-host <mirror-host>
    

    配置文件位置:Linux ~/.config/pip/pip.conf、macOS ~/Library/Application Support/pip/pip.conf、Windows %APPDATA%\pip\pip.ini

5) 进阶安装方式(本地、VCS、可编辑)

  • 本地目录/文件

    python -m pip install .
    python -m pip install ./dist/pkg-1.0.0-py3-none-any.whl
    
  • Git 仓库(PEP 508 直接引用)

    pkgname @ git+https://github/user/repo@v1.2.3
    

    或命令:

    python -m pip install "pkgname @ git+https://github/user/repo@v1.2.3"
    
  • 可编辑安装(本地开发)(PEP 660):

    python -m pip install -e .
    

    需使用现代 pyproject.toml 后端(如 setuptools, hatchling, poetry-core)。

6) wheel / sdist / 构建隔离(理解“为什么会编译失败”)

  • wheel:预构建包(安装最快)。pip 会优先挑与你平台/ABI 匹配的 wheel(如 manylinux, musllinux, win_amd64)。

  • sdist:源码分发;若无可用 wheel,pip 会在构建隔离环境里编译(需编译工具链与头文件)。

  • 常见解决:

    • 安装系统依赖(如编译器、Python 头文件、C 库)。

    • 强制选择/禁用二进制:

      python -m pip install --only-binary=:all: <pkg>   # 只接受二进制
      python -m pip install --no-binary=:all: <pkg>     # 强制源码构建
      
    • 对极端情况:--no-build-isolation(不隔离构建,复用当前环境的构建依赖),需确保已手动安装构建依赖。

7) 代理、离线与缓存

  • 代理

    python -m pip install --proxy http://user:pass@host:port <pkg>
    # 或环境变量:HTTP_PROXY / HTTPS_PROXY / NO_PROXY
    
  • 离线/可重复安装

    # 先在联网机器下载
    python -m pip download -r requirements.txt -d ./packages
    # 离线机器安装(不访问索引)
    python -m pip install --no-index --find-links=./packages -r requirements.txt
    
  • 缓存

    python -m pip cache dir
    python -m pip cache list
    python -m pip cache purge
    

8) 预发布/可选依赖/平台标记

  • 预发布版本

    python -m pip install --pre "pack>=2.0b1"
    
  • 可选依赖(extras)

    python -m pip install "requests[socks]"
    
  • 平台标记:在 requirements 中可以用环境标记控制条件安装:

    backports.zoneinfo; python_version < "3.9"
    

9) 安全与合规(非常重要)

  • 哈希校验(防篡改)

    # 强制所有依赖必须带哈希(requirements.txt 中附带 --hash)
    python -m pip install --require-hashes -r requirements.txt
    

    (用 pip-compile --generate-hashes 生成带哈希的锁文件最方便)

  • 审计:结合 pip-audit 或 SCA 工具在 CI 中扫描漏洞数据库(OSV 等)。

  • 最小权限:禁用可疑 post-install 网络脚本、最小化构建步骤;生产只用锁定清单并固定索引。

  • 减少混源:优先单代理;如必须混源,确保命名空间策略与优先级正确,防止 dependency confusion

10) 常见问题速排

  1. 找不到编译环境 / Failed building wheel

    • 缺编译器或 C 头文件。Linux 安装 build-essential/等;Windows 需 “VS Build Tools”;或改装预编译 wheel(换 Python/平台版本)。
  2. SSL: CERTIFICATE_VERIFY_FAILED

    • 系统 CA 证书缺失/过期;更新系统证书或设 --cert 指定 CA(勿随意 --trusted-host,除非受控内网)。
  3. Resolution/backtracking 很慢

    • 放宽/收紧版本约束;分层 requirements;用 pip-tools 预解析。
  4. 版本/解释器不兼容(Requires-Python)

    • 查看 pip show <pkg> 或错误提示;升级/切换 Python 版本(pyenv)。
  5. “not on PATH / Scripts vs bin”

    • 虚拟环境的 bin/(Unix)或 Scripts\(Win)不在 PATH;重新激活 venv。
  6. 系统 Python 被锁(Debian/Ubuntu)

    • 避免全局安装;使用 venv。若必须全局装,部分系统需要 --break-system-packages不推荐)。

11) 与 pip 相关的周边工具

  • pip-tools:专业锁定与同步(企业强推)。

  • poetry:一体化(依赖解析、构建、发布)。

  • pipx:把CLI 工具装到独立隔离环境(避免污染项目/全局)。

    python -m pip install pipx && pipx install httpie
    

12) 小抄(可直接贴到 README)

# 基础
python -m venv .venv && source .venv/bin/activate
python -m pip install -U pip
python -m pip install -r requirements.txt
python -m pip list --outdated
python -m pip check

# 锁定(pip-tools)
python -m pip install pip-tools
pip-compile --generate-hashes
pip-sync

# 指定源与离线
python -m pip install -i https://<proxy>/simple -r requirements.txt
python -m pip download -r requirements.txt -d ./vendor
python -m pip install --no-index --find-links=./vendor -r requirements.txt

# VCS / 编辑模式
python -m pip install -e .
python -m pip install "pkg @ git+https://github/org/repo@v1.2.3"

# 安全
python -m pip install --require-hashes -r requirements.txt

pip(Python 的包安装器) 从概念到实战、再到安全与排错一次讲清。你可以把这当成一份随时可抄的“pip 作业手册”。

1) pip 是什么 & 正确调用方式

  • 作用:从索引(默认 PyPI)下载并安装wheel/sdist 包,解析依赖、卸载、查询等。

  • 推荐调用始终用你要用的解释器来调用——避免多 Python 冲突:

    python -m pip --version
    python -m pip install <pkg>
    

    pip/pip3 可能指向别的解释器;用 python -m pip 最稳。

2) 基本安装与虚拟环境(强烈建议)

# 创建隔离环境(Linux/macOS)
python -m venv .venv && source .venv/bin/activate
# Windows
python -m venv .venv && .\.venv\Scripts\activate

# 安装/升级
python -m pip install -U pip             # 升级 pip 自身
python -m pip install requests           # 安装
python -m pip uninstall requests         # 卸载
python -m pip show requests              # 查看详情
python -m pip list --outdated            # 查看可升级包
python -m pip check                      # 检查依赖是否冲突/缺失

不要在系统 Python 全局装包;必要时用 --user(装到“用户 site-packages”)或开启虚拟环境。

3) requirements / constraints / 锁定

  • requirements.txt:要安装的包清单(可写版本范围或固定版本)。

  • constraints 文件:只约束版本,不会被直接安装;与多个 requirements 共享约束非常有用。

    python -m pip install -r requirements.txt -c constraints.txt
    
  • 固定依赖(可复现构建)

    • 简单:pip freeze > requirements.txt(记录当前环境的全部精确版本)。

    • 更专业:pip-tools 生成锁定版本(含哈希):

      python -m pip install pip-tools
      pip-compile --generate-hashes       # 从 requirements.in 解析并锁定
      pip-sync                            # 按锁文件同步环境
      

4) 选择索引/镜像与私有仓

  • 临时切换:

    python -m pip install -i https://pypi/simple <pkg>
    python -m pip install --index-url https://<私有代理>/simple <pkg>
    python -m pip install --extra-index-url https://<次级源>/simple <pkg>
    

    安全建议:优先使用单一私有代理(代理上游 PyPI),减少“依赖混源”引发的 dependency confusion 风险;如必须混源,确保私有源在前,并对私有命名空间做白名单。

  • 永久配置(按 OS):

    # 查看/设置
    python -m pip config list
    python -m pip config set global.index-url https://<mirror>/simple
    python -m pip config set global.trusted-host <mirror-host>
    

    配置文件位置:Linux ~/.config/pip/pip.conf、macOS ~/Library/Application Support/pip/pip.conf、Windows %APPDATA%\pip\pip.ini

5) 进阶安装方式(本地、VCS、可编辑)

  • 本地目录/文件

    python -m pip install .
    python -m pip install ./dist/pkg-1.0.0-py3-none-any.whl
    
  • Git 仓库(PEP 508 直接引用)

    pkgname @ git+https://github/user/repo@v1.2.3
    

    或命令:

    python -m pip install "pkgname @ git+https://github/user/repo@v1.2.3"
    
  • 可编辑安装(本地开发)(PEP 660):

    python -m pip install -e .
    

    需使用现代 pyproject.toml 后端(如 setuptools, hatchling, poetry-core)。

6) wheel / sdist / 构建隔离(理解“为什么会编译失败”)

  • wheel:预构建包(安装最快)。pip 会优先挑与你平台/ABI 匹配的 wheel(如 manylinux, musllinux, win_amd64)。

  • sdist:源码分发;若无可用 wheel,pip 会在构建隔离环境里编译(需编译工具链与头文件)。

  • 常见解决:

    • 安装系统依赖(如编译器、Python 头文件、C 库)。

    • 强制选择/禁用二进制:

      python -m pip install --only-binary=:all: <pkg>   # 只接受二进制
      python -m pip install --no-binary=:all: <pkg>     # 强制源码构建
      
    • 对极端情况:--no-build-isolation(不隔离构建,复用当前环境的构建依赖),需确保已手动安装构建依赖。

7) 代理、离线与缓存

  • 代理

    python -m pip install --proxy http://user:pass@host:port <pkg>
    # 或环境变量:HTTP_PROXY / HTTPS_PROXY / NO_PROXY
    
  • 离线/可重复安装

    # 先在联网机器下载
    python -m pip download -r requirements.txt -d ./packages
    # 离线机器安装(不访问索引)
    python -m pip install --no-index --find-links=./packages -r requirements.txt
    
  • 缓存

    python -m pip cache dir
    python -m pip cache list
    python -m pip cache purge
    

8) 预发布/可选依赖/平台标记

  • 预发布版本

    python -m pip install --pre "pack>=2.0b1"
    
  • 可选依赖(extras)

    python -m pip install "requests[socks]"
    
  • 平台标记:在 requirements 中可以用环境标记控制条件安装:

    backports.zoneinfo; python_version < "3.9"
    

9) 安全与合规(非常重要)

  • 哈希校验(防篡改)

    # 强制所有依赖必须带哈希(requirements.txt 中附带 --hash)
    python -m pip install --require-hashes -r requirements.txt
    

    (用 pip-compile --generate-hashes 生成带哈希的锁文件最方便)

  • 审计:结合 pip-audit 或 SCA 工具在 CI 中扫描漏洞数据库(OSV 等)。

  • 最小权限:禁用可疑 post-install 网络脚本、最小化构建步骤;生产只用锁定清单并固定索引。

  • 减少混源:优先单代理;如必须混源,确保命名空间策略与优先级正确,防止 dependency confusion

10) 常见问题速排

  1. 找不到编译环境 / Failed building wheel

    • 缺编译器或 C 头文件。Linux 安装 build-essential/等;Windows 需 “VS Build Tools”;或改装预编译 wheel(换 Python/平台版本)。
  2. SSL: CERTIFICATE_VERIFY_FAILED

    • 系统 CA 证书缺失/过期;更新系统证书或设 --cert 指定 CA(勿随意 --trusted-host,除非受控内网)。
  3. Resolution/backtracking 很慢

    • 放宽/收紧版本约束;分层 requirements;用 pip-tools 预解析。
  4. 版本/解释器不兼容(Requires-Python)

    • 查看 pip show <pkg> 或错误提示;升级/切换 Python 版本(pyenv)。
  5. “not on PATH / Scripts vs bin”

    • 虚拟环境的 bin/(Unix)或 Scripts\(Win)不在 PATH;重新激活 venv。
  6. 系统 Python 被锁(Debian/Ubuntu)

    • 避免全局安装;使用 venv。若必须全局装,部分系统需要 --break-system-packages不推荐)。

11) 与 pip 相关的周边工具

  • pip-tools:专业锁定与同步(企业强推)。

  • poetry:一体化(依赖解析、构建、发布)。

  • pipx:把CLI 工具装到独立隔离环境(避免污染项目/全局)。

    python -m pip install pipx && pipx install httpie
    

12) 小抄(可直接贴到 README)

# 基础
python -m venv .venv && source .venv/bin/activate
python -m pip install -U pip
python -m pip install -r requirements.txt
python -m pip list --outdated
python -m pip check

# 锁定(pip-tools)
python -m pip install pip-tools
pip-compile --generate-hashes
pip-sync

# 指定源与离线
python -m pip install -i https://<proxy>/simple -r requirements.txt
python -m pip download -r requirements.txt -d ./vendor
python -m pip install --no-index --find-links=./vendor -r requirements.txt

# VCS / 编辑模式
python -m pip install -e .
python -m pip install "pkg @ git+https://github/org/repo@v1.2.3"

# 安全
python -m pip install --require-hashes -r requirements.txt

本文标签: 详解作答工具pipChatGpt