注:本文为 “Python 包管理 | Pip 与 Pipx” 相关合辑。
英文引文,机翻未校。
中文引文,略作重排。
未整理去重。
如有内容异常,请看原文。
Pip vs Pipx: Complete Guide to Python Package Management
Pip 与 Pipx:Python 包管理完整指南
Stanley Ulili
Updated on June 20, 2025
Python has two main tools for installing packages: pip and pipx. They both install Python packages, but they work very differently and solve different problems.
Python 有两个主要的包安装工具:pip 和 pipx。它们都能安装 Python 包,但工作方式大不相同,解决的问题也不一样。
pip is Python’s standard package installer. It comes built into Python and handles all kinds of packages - from libraries you use in your code to development tools. You’ll use pip when you need to install packages for your Python projects.
pip 是 Python 的标准包安装程序。它内置在 Python 中,可处理各种包——从代码中使用的库到开发工具。当你需要为 Python 项目安装包时,就会用到 pip。
pipx takes a different approach. It only installs Python applications that you run from the command line. Each app gets its own isolated environment, so they can’t interfere with each other or break your other tools.
pipx 采用了不同的方法。它只安装从命令行运行的 Python 应用程序。每个应用都有自己的隔离环境,因此它们不会相互干扰或破坏你的其他工具。
This guide will show you when to use each tool and help you avoid common mistakes that can mess up your Python setup.
本指南将告诉你何时使用每种工具,并帮助你避免可能搞乱 Python 配置的常见错误。
What is pip?
什么是 pip?
pip is Python’s main package installer. It’s been around since 2008 and comes pre-installed with Python versions 3.4 and newer.
pip 是 Python 的主要包安装程序。它自 2008 年问世以来,在 Python 3.4 及更高版本中预安装。
When you install Python, you automatically get pip. It can install packages from PyPI (the Python Package Index) and other sources. pip handles dependencies, manages different package versions, and works with virtual environments.
安装 Python 时,你会自动获得 pip。它可以从 PyPI(Python 包索引)和其他来源安装包。pip 处理依赖项、管理不同的包版本,并且可与虚拟环境配合使用。
pip gives you complete control over what gets installed and where. You can install packages globally on your system, in virtual environments, or with specific version requirements. This flexibility makes pip essential for Python development, but it also means you need to understand how to use it properly.
pip 让你完全控制安装的内容和位置。你可以在系统全局、虚拟环境中安装包,或者按特定的版本要求安装。这种灵活性使 pip 成为 Python 开发的必备工具,但这也意味着你需要了解如何正确使用它。
What is pipx?
什么是 pipx?
pipx solves a specific problem: installing Python command-line tools without breaking your system or other tools.
pipx 解决了一个特定问题:安装 Python 命令行工具时不会破坏你的系统或其他工具。
When you install a tool like black
or flake8
with pipx, it creates a separate virtual environment just for that tool. The tool becomes available system-wide, but its dependencies stay isolated. This means you can install multiple tools that need different versions of the same library without any conflicts.
当你用 pipx 安装 black
或 flake8
之类的工具时,它会为该工具创建一个单独的虚拟环境。该工具可在系统全局使用,但其依赖项保持隔离。这意味着你可以安装多个需要同一库不同版本的工具,而不会产生任何冲突。
pipx focuses only on applications - tools you run from the command line. You can’t use pipx to install libraries that you import in your Python code. It’s designed for end-user applications like code formatters, linters, and other command-line utilities.
pipx 只专注于应用程序——即你从命令行运行的工具。你不能用 pipx 安装在 Python 代码中导入的库。它是为终端用户应用程序设计的,如代码格式化工具、代码检查工具和其他命令行实用程序。
pip vs. pipx: a detailed comparison
pip 与 pipx:详细对比
Understanding when to use each tool requires looking at what they’re designed to do. While both install Python packages, they serve completely different purposes.
要了解何时使用每种工具,需要看它们的设计用途。虽然两者都能安装 Python 包,但它们的用途完全不同。
Here are the key differences that matter:
以下是重要的主要区别:
Aspect | pip | pipx |
---|---|---|
Primary purpose 主要用途 | Library and package management 库和包管理 | Application installation and execution 应用程序安装和执行 |
Installation scope 安装范围 | Project-specific or global 特定项目或全局 | Isolated applications with global access 具有全局访问权限的隔离应用程序 |
Dependency management 依赖项管理 | Manual virtual environment handling 手动处理虚拟环境 | Automatic isolation per application 每个应用程序自动隔离 |
Target use case 目标用例 | Development dependencies and libraries 开发依赖项和库 | Command-line tools and standalone apps 命令行工具和独立应用程序 |
Environment handling 环境处理 | Requires manual venv management 需要手动管理虚拟环境 | Creates isolated environments automatically 自动创建隔离环境 |
Package types 包类型 | Any Python package or library 任何 Python 包或库 | Applications with console scripts 带控制台脚本的应用程序 |
Upgrade strategy 升级策略 | Manual dependency resolution 手动解决依赖项 | Per-application upgrade isolation 每个应用程序独立升级 |
Configuration complexity 配置复杂度 | Flexible but requires setup 灵活但需要设置 | Minimal configuration needed 需极少配置 |
Development workflow 开发工作流 | Integrates with requirements.txt 与 requirements.txt 集成 | Designed for end-user tool installation 专为终端用户工具安装设计 |
Conflict resolution 冲突解决 | User manages conflicts manually 用户手动管理冲突 | Prevents conflicts through isolation 通过隔离防止冲突 |
System integration 系统集成 | Installs to active environment 安装到活动环境 | Creates system-wide accessible commands 创建系统全局可访问的命令 |
Temporary execution 临时执行 | Requires installation first 需先安装 | Supports run-once execution with --run 支持使用 --run 进行一次性执行 |
Maintenance overhead 维护开销 | Requires environment management 需要管理环境 | Self-contained application management 独立的应用程序管理 |
Installation methods
安装方法
Installing these tools shows you their fundamental differences and where you’ll use them.
安装这些工具的过程能体现它们的根本区别以及适用场景。
pip comes with Python, so you probably already have it. If you’re using Python 3.4 or newer, pip is ready to use. For older Python versions or custom setups, you might need to install it separately.
pip 随 Python 一起提供,所以你可能已经拥有它了。如果你使用的是 Python 3.4 或更高版本,pip 已可直接使用。对于较旧的 Python 版本或自定义设置,你可能需要单独安装它。
# Check if pip is installed // 检查 pip 是否安装
python -m pip --version # Display pip version if installed // 若已安装则显示 pip 版本
# Install pip if you don't have it // 如果没有 pip,安装它
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py # Download pip installation script // 下载 pip 安装脚本
python get-pip.py # Run installation script // 运行安装脚本
# Install via system package managers // 通过系统包管理器安装
# Ubuntu/Debian
sudo apt update && sudo apt install python3-pip # Install pip for Python 3 via APT // 通过 APT 安装 Python 3 的 pip
# macOS with Homebrew
brew install python3 # includes pip // 安装 Python 3(包含 pip)
pip works directly with Python’s import system. When you install a package with pip, it goes into your current Python environment - whether that’s your system Python, a virtual environment, or a conda environment.
pip 直接与 Python 的导入系统配合工作。当你用 pip 安装包时,它会进入你当前的 Python 环境——无论是系统 Python、虚拟环境还是 conda 环境。
pipx is a separate tool that you need to install. The easiest way is to use pip to install pipx, then set up the command paths.
pipx 是一个需要单独安装的工具。最简单的方法是用 pip 安装 pipx,然后设置命令路径。
# Install pipx using pip // 使用 pip 安装 pipx
python -m pip install --user pipx # Install pipx to user directory // 将 pipx 安装到用户目录
python -m pipx ensurepath # Add pipx to system PATH // 将 pipx 添加到系统路径
# Alternative installation methods // 其他安装方法
# Ubuntu/Debian
sudo apt install pipx # Install via APT package manager // 通过 APT 包管理器安装
# macOS with Homebrew
brew install pipx # Install via Homebrew package manager // 通过 Homebrew 包管理器安装
# Windows with Scoop
scoop install pipx # Install via Scoop package manager // 通过 Scoop 包管理器安装
# Check that it works // 检查是否安装成功
pipx --version # Display installed pipx version // 显示已安装的 pipx 版本
After you install pipx, it creates its own directory structure to manage isolated applications. This keeps pipx-managed tools completely separate from your development environments and system Python.
安装 pipx 后,它会创建自己的目录结构来管理隔离的应用程序。这使得 pipx 管理的工具与你的开发环境和系统 Python 完全分离。
Package installation patterns
包安装模式
The way you install packages with each tool shows their different purposes and philosophies.
使用每种工具安装包的方式体现了它们不同的用途和设计理念。
pip installs packages into whatever Python environment is currently active. This could be your system Python, a virtual environment, or any other Python setup you’re using.
pip 会将包安装到当前激活的 Python 环境中。这可能是你的系统 Python、虚拟环境或你正在使用的任何其他 Python 配置。
# Basic package installation // 基本包安装
pip install requests # HTTP library // HTTP 库
# Install with version constraints // 按版本约束安装
pip install "django>=3.2,<4.0" # Web framework, version 3.2 or higher but less than 4.0 // Web 框架,版本 3.2 及以上但低于 4.0
# Install from requirements file // 从需求文件安装
pip install -r requirements.txt # Install multiple packages from list // 从列表安装多个包
# Install in development mode // 以开发模式安装
pip install -e . # Editable install for package development // 用于包开发的可编辑安装
# Install from Git repository // 从 Git 仓库安装
pip install git+https://github.com/user/repo.git # Install directly from source repository // 直接从源代码仓库安装
# Install with optional features // 安装带可选功能的包
pip install "fastapi[all]" # API framework with all optional components // 带所有可选组件的 API 框架
pip’s flexibility supports complex scenarios. You can install packages for development, production, testing, or any other purpose. The packages become part of your Python environment and you can import them in your code.
pip 的灵活性支持复杂场景。你可以为开发、生产、测试或任何其他目的安装包。这些包会成为你的 Python 环境的一部分,你可以在代码中导入它们。
pipx takes a completely different approach. It only installs applications that provide command-line tools. Each application gets its own isolated environment, but the commands become available system-wide.
pipx 采用了完全不同的方法。它只安装提供命令行工具的应用程序。每个应用程序都有自己的隔离环境,但命令可在系统全局使用。
# Install a command-line tool // 安装命令行工具
pipx install black
# Install with specific pip arguments // 使用特定的 pip 参数安装
pipx install --pip-args="--pre" black
# Install from Git repository // 从 Git 仓库安装
pipx install git+https://github.com/psf/black.git
# Install with optional features // 安装带可选功能的应用
pipx install "jupyter[all]"
# Run a tool without installing it // 不安装工具直接运行
pipx run black --check .
# Install with specific Python version // 使用特定的 Python 版本安装
pipx install --python python3.9 black
When you install black
with pipx, you can’t import it in your Python code. Instead, you get a black
command that you can run from anywhere on your system. The tool and all its dependencies stay isolated from everything else.
当你用 pipx 安装 black
时,你不能在 Python 代码中导入它。相反,你会得到一个 black
命令,可以从系统的任何地方运行它。该工具及其所有依赖项与其他所有内容保持隔离。
Virtual environment management
虚拟环境管理
Virtual environments are where pip and pipx show their biggest differences. This affects how much work you need to do and how likely you are to run into problems.
虚拟环境是 pip 和 pipx 体现最大差异的地方。这会影响你需要做的工作量以及遇到问题的可能性。
pip requires you to manage virtual environments manually if you want to isolate your projects. This gives you complete control but means you need to understand and remember to use virtual environments properly.
如果想隔离项目,pip 需要你手动管理虚拟环境。这让你拥有完全的控制权,但意味着你需要了解并记住正确使用虚拟环境。
# Manual virtual environment workflow // 手动虚拟环境工作流程
python -m venv project_env
source project_env/bin/activate # On Windows: project_env\Scripts\activate // Windows 系统:project_env\Scripts\activate
# Install project dependencies // 安装项目依赖项
pip install -r requirements.txt
# Work on your project // 处理项目
python main.py
# Deactivate when finished // 完成后退出虚拟环境
deactivate
# Development dependencies // 开发依赖项
pip install -r requirements-dev.txt
When you’re working on multiple projects, you need to manage multiple virtual environments. Each project should have its own environment to avoid conflicts.
当你处理多个项目时,需要管理多个虚拟环境。每个项目都应该有自己的环境以避免冲突。
# Managing multiple projects // 管理多个项目
python -m venv project1_env
python -m venv project2_env
python -m venv experiment_env
# Switch between environments // 在环境之间切换
source project1_env/bin/activate
# ... work on project1 // 处理 project1
deactivate
source project2_env/bin/activate
# ... work on project2 // 处理 project2
deactivate
You can use tools like virtualenvwrapper or conda to make this easier, but they add complexity to your setup.
你可以使用 virtualenvwrapper 或 conda 之类的工具来简化操作,但它们会增加配置的复杂性。
pipx handles all virtual environment management automatically. You never need to create, activate, or deactivate environments. Every application you install gets its own environment behind the scenes.
pipx 会自动处理所有虚拟环境管理。你永远不需要创建、激活或退出环境。你安装的每个应用程序在后台都有自己的环境。
# No environment management needed
pipx install black # Creates isolated environment automatically // 自动创建隔离环境
pipx install flake8 # Creates separate isolated environment // 创建单独的隔离环境
pipx install mypy # Creates another separate isolated environment // 创建另一个单独的隔离环境
# All tools work from anywhere // 所有工具可在任何地方使用
black --version
flake8 --version
mypy --version
# pipx manages everything for you // pipx 为你管理所有事情
pipx list # Shows all installed applications // 显示所有已安装的应用程序
pipx upgrade black # Upgrades black in its isolated environment // 在其隔离环境中升级 black
pipx uninstall flake8 # Removes flake8 and its environment completely // 完全移除 flake8 及其环境
You never have to think about environments with pipx. You can install tools that need conflicting dependencies without any problems because each tool lives in complete isolation.
使用 pipx 时,你永远不必考虑环境问题。你可以安装需要冲突依赖项的工具,且不会有任何问题,因为每个工具都处于完全隔离的状态。
Dependency conflict resolution
依赖项冲突解决
The approaches to managing dependency conflicts show the core differences between these tools and reveal their appropriate use cases.
管理依赖项冲突的方法体现了这些工具的核心差异,并揭示了它们的适用场景。
pip resolves dependencies within whatever environment you’re using. When multiple packages need different versions of the same dependency, pip tries to find a version that works for everyone. Sometimes this fails or forces upgrades that break your existing code.
pip 会在你正在使用的任何环境中解决依赖项问题。当多个包需要同一依赖项的不同版本时,pip 会尝试找到一个对所有包都适用的版本。有时这会失败,或者会强制升级,从而破坏你现有的代码。
# Potential conflict scenario // 潜在的冲突场景
pip install requests==2.25.1 # Installs requests 2.25.1
pip install some-package # Might require requests>=2.26.0
# pip tries to resolve by upgrading requests // pip 尝试通过升级 requests 来解决冲突
# This could break code that depends on requests 2.25.1 behavior // 这可能会破坏依赖于 requests 2.25.1 功能的代码
When conflicts happen, pip either refuses to install the package or forces changes that might break your existing setup. You need to carefully manage versions to avoid these problems.
当冲突发生时,pip 要么拒绝安装包,要么强制进行可能破坏你现有配置的更改。你需要仔细管理版本以避免这些问题。
# Complex dependency management // 复杂的依赖项管理
pip install "django>=3.2,<3.3"
pip install "requests>=2.25.0,<2.26.0"
pip install "urllib3>=1.26.0,<1.27.0"
# Your requirements file gets complicated // 你的需求文件会变得复杂
echo "django>=3.2,<3.3" >> requirements.txt
echo "requests>=2.25.0,<2.26.0" >> requirements.txt
echo "urllib3>=1.26.0,<1.27.0" >> requirements.txt
Even when you’re careful, indirect dependencies can cause unexpected conflicts that are hard to debug.
即使你很小心,间接依赖项也可能导致难以调试的意外冲突。
pipx eliminates dependency conflicts completely through isolation. Each application has its own environment with its own dependency versions. Applications can’t interfere with each other.
pipx 通过隔离完全消除了依赖项冲突。每个应用程序都有自己的环境和自己的依赖项版本。应用程序之间不会相互干扰。
# No conflicts possible - each app is isolated // 不可能有冲突——每个应用都相互隔离
pipx install tool-requiring-requests-2.25
pipx install tool-requiring-requests-2.28
pipx install tool-requiring-old-urllib3
# Each tool runs in its own environment // 每个工具在自己的环境中运行
pipx list
# Shows:
# tool-requiring-requests-2.25 (with requests 2.25.1)
# tool-requiring-requests-2.28 (with requests 2.28.0)
# tool-requiring-old-urllib3 (with urllib3 1.25.0)
This isolation works for all dependencies, including complex chains of requirements. The trade-off is that you use more disk space because each application stores its own copy of dependencies.
这种隔离适用于所有依赖项,包括复杂的需求链。但代价是占用更多磁盘空间,因为每个应用程序都存储自己的依赖项副本。
Use case scenarios
用例场景
Understanding when to use each tool helps you pick the right one for your specific needs and avoid common mistakes.
了解何时使用每种工具有助于你为特定需求选择合适的工具,并避免常见错误。
Use pip when you need to install libraries that your Python code will import and use. This includes packages for data analysis, web development, machine learning, or any other functionality you want to add to your programs.
当你需要安装 Python 代码将导入和使用的库时,请使用 pip。这包括用于数据分析、Web 开发、机器学习的包,或任何你想添加到程序中的其他功能包。
# Development scenario - these need pip installation // 开发场景——这些需要用 pip 安装
import requests # HTTP library for API calls // 用于 API 调用的 HTTP 库
import pandas # Data manipulation // 数据处理
import numpy # Numerical computing // 数值计算
import django # Web framework // Web 框架
from myproject import utils # Your project's modules // 你的项目模块
# These packages become part of your application // 这些包成为应用程序的一部分
response = requests.get('https://api.example.com/data ')
df = pandas.DataFrame(response.json())
pip also works for project-specific development tools that need to work with your codebase:
pip 也适用于需要与代码库配合使用的特定项目开发工具:
# Development tools installed with pip in project environment // 在项目环境中用 pip 安装的开发工具
pip install pytest pytest-cov # Testing framework // 测试框架
pip install black isort # Code formatting (project-specific versions) // 代码格式化(特定项目版本)
pip install mypy # Type checking // 类型检查
pip install sphinx # Documentation generation // 文档生成
# These tools work with your project's configuration files // 这些工具与项目的配置文件配合使用
pytest tests/
black --config pyproject.toml src/
mypy --config-file mypy.ini src/
Use pipx when you want to install standalone command-line applications. These are tools that you run from the terminal, not libraries that you import in your code.
当你想安装独立的命令行应用程序时,请使用 pipx。这些是你从终端运行的工具,而不是你在代码中导入的库。
# System-wide development tools // 系统全局开发工具
pipx install black # Code formatter // 代码格式化工具
pipx install flake8 # Linter // 代码检查工具
pipx install mypy # Type checker // 类型检查工具
pipx install pre-commit # Git hooks manager // Git 钩子管理器
pipx install cookiecutter # Project template generator // 项目模板生成器
# Utility and multimedia tools // 实用工具和多媒体工具
pipx install youtube-dl # Video downloader // 视频下载器
pipx install speedtest-cli # Internet speed testing // 网速测试工具
pipx install httpie # HTTP client // HTTP 客户端
pipx install rich-cli # Terminal formatting // 终端格式化工具
# Data science and analysis tools // 数据科学和分析工具
pipx install jupyter # Notebook environment // 笔记本环境
pipx install streamlit # Web app framework // Web 应用框架
pipx install datasette # Database exploration // 数据库探索工具
The key difference: pip-installed packages become part of your Python environment and you can import them in your code. pipx-installed applications give you commands that work independently of your development environment.
关键区别:用 pip 安装的包成为 Python 环境的一部分,你可以在代码中导入它们。用 pipx 安装的应用程序提供的命令独立于你的开发环境运行。
Performance and resource considerations
性能和资源考量
These tools use system resources differently, which affects storage requirements and performance in different ways.
这些工具使用系统资源的方式不同,这会以不同方式影响存储需求和性能。
pip shares dependencies between packages in the same environment. When multiple packages need the same library, pip installs it once and all packages use that shared copy. This saves disk space but can create version conflicts.
pip 在同一环境中的包之间共享依赖项。当多个包需要同一库时,pip 只安装一次,所有包都使用这个共享副本。这节省了磁盘空间,但可能会产生版本冲突。
# Shared dependencies with pip // pip 的共享依赖项
pip install django # Web framework // 安装 Django 及其依赖项
pip install django-rest-framework # REST API toolkit // 复用已安装的 Django
pip install celery # Asynchronous task queue // 可能与 Django 共享一些依赖项
# Efficient storage but potential for conflicts // 存储高效但可能存在冲突
pip list | grep -E "(django|celery|rest)"
# Shows the shared dependency versions // 显示共享的依赖项版本
When you use virtual environments with pip, you create separate copies of dependencies for each project. This uses more storage but gives you proper isolation.
当你将 pip 与虚拟环境一起使用时,会为每个项目创建单独的依赖项副本。这占用更多存储,但能提供适当的隔离。
# Multiple projects with pip // 使用 pip 的多个项目
# Project 1 // 项目 1
python -m venv project1_env
source project1_env/bin/activate
pip install django==3.2 # Web framework, v3.2 // Django 安装在 project1_env 中
# Project 2 // 项目 2
deactivate
python -m venv project2_env
source project2_env/bin/activate
pip install django==4.0 # Web framework, v4.0 // Django 单独安装在 project2_env 中
pipx prioritizes isolation over storage efficiency. Each application gets its own environment with its own copy of all dependencies. This uses more disk space but prevents all conflicts.
pipx 优先考虑隔离而非存储效率。每个应用程序都有自己的环境和所有依赖项的副本。这占用更多磁盘空间,但能防止所有冲突。
# pipx resource usage // pipx 的资源使用
pipx install black # Code formatter // 创建包含 black 及其所有依赖项的环境
pipx install isort # Import sorter // 创建包含 isort 及其所有依赖项的单独环境
pipx install mypy # Type checker // 创建包含 mypy 及其所有依赖项的单独环境
# Each application is completely isolated // 每个应用程序完全隔离
pipx list --verbose
# Shows detailed information about each isolated environment // 显示每个隔离环境的详细信息
The storage cost becomes noticeable with applications that have many dependencies:
对于有很多依赖项的应用程序,存储成本会很明显:
# Applications with heavy dependencies // 具有大量依赖项的应用程序
pipx install jupyter # Notebook environment // 创建包含笔记本生态系统的大型环境
pipx install tensorflow-cpu # Machine learning framework // 创建包含机器学习库的庞大环境
pipx install ansible # Automation tool // 创建包含自动化依赖项的环境
# Each maintains complete isolation // 每个都保持完全隔离
du -sh ~/.local/share/pipx/venvs/*
# Shows disk usage for each application environment // 显示每个应用程序环境的磁盘使用情况
Integration with development workflows
与开发工作流的集成
The way these tools fit into your daily development work affects your productivity and how easy it is to maintain your projects.
这些工具融入日常开发工作的方式会影响你的生产力以及项目维护的难易程度。
pip integrates deeply with Python development workflows. It supports requirements files, editable installations, and complex dependency specifications that you need for reproducible development environments.
pip 与 Python 开发工作流深度集成。它支持需求文件、可编辑安装以及可重现的开发环境所需的复杂依赖项规范。
# Development workflow integration // 开发工作流集成
pip install -e . # Editable mode for active development // 开发用的可编辑安装
pip install -r requirements.txt # Dependencies for production environment // 生产环境依赖项
pip install -r requirements-dev.txt # Dependencies for development environment // 开发环境依赖项
pip freeze > requirements-lock.txt # Generate file with exact dependency versions // 创建精确版本的锁定文件
# CI/CD integration // 与 CI/CD 集成
pip install --no-deps -r requirements-lock.txt # Exact environment replication // 精确重现环境
pip check # Validate dependency compatibility // 验证依赖项是否兼容
pip supports sophisticated dependency management through configuration files:
pip 通过配置文件支持复杂的依赖项管理:
# setup.py or pyproject.toml integration \\ 与 setup.py 或 pyproject.toml 集成
[build-system]
requires = ["setuptools>=45", "wheel"]
[project]
dependencies = [
"requests>=2.25.0",
"click>=8.0.0",
]
[project.optional-dependencies]
dev = [
"pytest>=6.0",
"black>=22.0",
"mypy>=0.900",
]
pipx complements development workflows by giving you consistent tooling that doesn’t interfere with your project dependencies. You can have stable tool versions regardless of what your individual projects need.
pipx 通过提供一致的工具(不会干扰项目依赖项)来补充开发工作流。无论各个项目需要什么,你都可以拥有稳定的工具版本。
# Stable development tooling // 稳定的开发工具
pipx install black==22.3.0 # Code formatter, v22.3.0 // 所有项目使用相同的格式化工具版本
pipx install mypy==0.950 # Type checker, v0.950 // 所有地方使用相同的类型检查工具版本
pipx install pre-commit==2.19.0 # Git hooks manager, v2.19.0 // 所有项目使用相同的 git 钩子版本
# Project-specific configurations still work // 特定项目的配置仍然有效
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: black
name: black
entry: black # Uses pipx-installed black // 使用 pipx 安装的 black
language: system
This separation lets teams standardize on tool versions while allowing projects to have different library requirements:
这种分离允许团队统一工具版本,同时允许项目有不同的库要求:
# Team standardization with pipx // 使用 pipx 实现团队标准化
pipx install black==22.3.0 # Code formatter, v22.3.0 // 整个团队使用相同的格式化工具
pipx install isort==5.10.1 # Import sorter, v5.10.1 // 所有人使用相同的导入排序工具
# Project flexibility with pip // 使用 pip 实现项目灵活性
# Project A // 项目 A
pip install django==3.2.13 # Web framework, v3.2.13 // 旧项目使用较旧的 Django 版本
# Project B // 项目 B
pip install django==4.0.4 # Web framework, v4.0.4 // 新项目使用较新的 Django 版本
Package discovery and exploration
包的发现与探索
Package discovery and exploration approaches reflect the different use cases and target audiences of each tool.
包的发现和探索方法反映了每种工具的不同用例和目标受众。
pip gives you comprehensive package discovery through direct PyPI integration. You can search for packages, get detailed information, and explore dependencies.
pip 通过直接与 PyPI 集成,为你提供全面的包发现功能。你可以搜索包、获取详细信息并探索依赖项。
# Package discovery with pip // 使用 pip 发现包
pip show django # Detailed package information // 详细的包信息
pip show --verbose requests # Extended package details including files // 扩展的包详情,包括文件
pip list # List all installed packages // 列出所有已安装的包
pip list --outdated # Show packages that need updates // 显示需要更新的包
# Dependency exploration // 依赖项探索
pip show --verbose django | grep Requires # Show dependencies of django // 显示 django 的依赖项
pip show --verbose django | grep Required-by # Show packages dependent on django // 显示依赖 django 的包
pip works with tools that help you understand and manage dependencies:
pip 与帮助你理解和管理依赖项的工具配合使用:
# Advanced dependency exploration // 高级依赖项探索
pip install pipdeptree # Dependency visualization tool // 用于可视化依赖项的工具
pipdeptree # Display full dependency tree // 显示完整的依赖项树
pipdeptree --packages django # Focus on dependencies of specific package // 聚焦特定包的依赖项
pipdeptree --reverse # Show reverse dependencies (what depends on each package) // 显示每个包被哪些包依赖
pipx focuses on application discovery and management. It provides tools specifically designed for exploring and managing command-line applications.
pipx 专注于应用程序的发现和管理。它提供专为探索和管理命令行应用程序设计的工具。
# Application discovery with pipx // 使用 pipx 发现应用程序
pipx list # Show all installed applications // 显示所有已安装的应用程序
pipx list --verbose # Detailed information about each application // 每个应用程序的详细信息
pipx list --include-deps # Show applications and their dependencies // 显示应用程序及其依赖项
# Application exploration // 应用程序探索
pipx info black # Detailed information about black // 关于 black 的详细信息
pipx info --verbose jupyter # Extended details about jupyter installation // 关于 jupyter 安装的扩展详情
pipx has a unique feature that lets you try applications without installing them permanently:
pipx 有一个独特功能,允许你试用应用程序而无需永久安装它们:
# Try before installing // 先试后装
pipx run black --help # Run black without installing it // 不安装 black 直接运行
pipx run --spec black==22.3.0 black --version # Run specific version temporarily // 临时运行特定版本
pipx run cowsay "Hello World" # Try novelty applications without commitment // 试用新奇应用程序,无需承诺安装
# Temporary exploration // 临时探索
pipx run httpie httpie.io/hello # Make HTTP request without installing httpie // 不安装 httpie 直接发送 HTTP 请求
pipx run youtube-dl --help # Explore youtube-dl options without installing // 不安装 youtube-dl 直接探索其选项
Upgrade and maintenance strategies
升级和维护策略
Package maintenance approaches show how each tool handles the ongoing challenge of keeping your software current and secure.
包的维护方法显示了每种工具如何应对保持软件更新和安全的持续挑战。
pip gives you detailed control over upgrades, but you need to manually manage dependency relationships and potential conflicts that might arise.
pip 让你对升级有详细的控制,但你需要手动管理依赖关系和可能出现的潜在冲突。
# Individual package upgrades // 单个包升级
pip install --upgrade requests # Upgrade single package // 升级单个包
pip install --upgrade --no-deps numpy # Upgrade without touching dependencies // 升级时不改动依赖项
pip install --upgrade-strategy eager # Upgrade all dependencies aggressively // 激进地升级所有依赖项
# Bulk upgrade operations // 批量升级操作
pip list --outdated # Show packages needing updates // 显示需要更新的包
pip freeze | grep -v "^-e" | cut -d = -f 1 | xargs pip install -U # Upgrade everything // 升级所有包
# Careful upgrade with constraints // 带约束的谨慎升级
pip install --upgrade "django>=3.2,<4.0" # Constrained upgrade to avoid breaking changes // 带约束升级以避免破坏性变更
Managing upgrades safely with pip requires understanding how changes might affect your dependencies:
使用 pip 安全地管理升级需要了解变更可能如何影响你的依赖项:
# Safe upgrade workflow // 安全升级工作流
pip check # Verify current installation is consistent // 验证当前安装是否一致
pip list --outdated > outdated.txt # Record what needs updating // 记录需要更新的内容
pip install --upgrade package-name # Upgrade individual packages // 升级单个包
pip check # Verify no conflicts were introduced // 验证未引入冲突
pipx simplifies upgrades through application-level isolation. Since each application lives in its own environment, upgrades can’t cause conflicts between different tools.
pipx 通过应用程序级别的隔离简化了升级。由于每个应用程序都在自己的环境中,升级不会导致不同工具之间的冲突。
# Simple application upgrades // 简单的应用程序升级
pipx upgrade black # Upgrade single application safely // 安全升级单个应用程序
pipx upgrade-all # Upgrade all applications at once // 一次性升级所有应用程序
pipx upgrade --pip-args="--pre" mypy # Upgrade with specific pip arguments // 使用特定的 pip 参数升级
# Safe upgrade rollback // 安全的升级回滚
pipx list # Note current versions before upgrading // 升级前记录当前版本
pipx upgrade jupyter # Upgrade jupyter // 升级 jupyter
# If problems occur: // 如果出现问题:
pipx uninstall jupyter # Remove problematic version completely // 完全移除有问题的版本
pipx install jupyter==previous.version # Reinstall known good version // 重新安装已知正常的版本
pipx’s isolation means you can upgrade tools fearlessly without worrying about breaking other applications:
pipx 的隔离意味着你可以大胆升级工具,而不必担心破坏其他应用程序:
# Fearless upgrades // 放心升级
pipx upgrade black # Won't affect any other tools // 不会影响任何其他工具
pipx upgrade --include-deps mypy # Upgrades mypy and dependencies safely // 安全升级 mypy 及其依赖项
pipx upgrade pre-commit # Won't interfere with other project tooling // 不会干扰其他项目工具
This isolation lets you use more aggressive update strategies without fear of breaking your existing setup.
这种隔离使你可以采用更激进的更新策略,而不必担心破坏现有的配置。
Security considerations
安全考量
Security implications differ between the tools based on how they install packages and manage environments.
基于安装包和管理环境的方式不同,这些工具的安全影响也不同。
pip installations share the security context of their target environment. If a compromised package gets installed, it can potentially affect other packages in the same environment or access system resources.
pip 安装的包共享其目标环境的安全上下文。如果安装了受感染的包,它可能会影响同一环境中的其他包或访问系统资源。
# Security considerations with pip // 使用 pip 的安全考量
pip install --user package-name # Install to user directory (safer than system-wide) // 安装到用户目录(比系统全局更安全)
pip install --no-deps package-name # Skip dependencies for manual security review // 跳过依赖项以进行手动安全审查
pip install --require-hashes -r requirements.txt # Verify package integrity with hashes // 使用哈希验证包的完整性
# Security scanning // 安全扫描
pip-audit # Scan installed packages for known vulnerabilities // 扫描已安装包的已知漏洞
pip show package-name # Verify package details and origin // 验证包的详情和来源
Global pip installations create particular security risks because they affect your entire system:
全局 pip 安装会带来特殊的安全风险,因为它们会影响你的整个系统:
# Risky global installation // 有风险的全局安装
sudo pip install package-name # Installs system-wide with elevated privileges // 以提升的权限在系统全局安装
# A compromised package could affect system Python or other global packages // 受感染的包可能会影响系统 Python 或其他全局包
Virtual environments provide some protection but still share the base Python installation:
虚拟环境提供一定的保护,但仍共享基础 Python 安装:
# Safer pip usage with virtual environments // 在虚拟环境中更安全地使用 pip
python -m venv secure_env
source secure_env/bin/activate
pip install package-name # Isolated from system but shares Python interpreter // 与系统隔离,但共享 Python 解释器
pipx provides stronger security isolation through complete environment separation. Each application runs in its own sandbox, limiting the damage a compromised package can cause.
pipx 通过完全的环境分离提供更强的安全隔离。每个应用程序在自己的沙箱中运行,限制受感染包可能造成的损害。
# Enhanced security through isolation // 通过隔离增强安全性
pipx install untrusted-tool # Runs in completely isolated environment // 在完全隔离的环境中运行
pipx run suspicious-package --help # Temporary execution without permanent installation // 临时执行,不永久安装
pipx install --pip-args="--no-deps" careful-package # Skip dependencies if needed // 如有需要,跳过依赖项
Each pipx application runs in complete isolation, so a compromised application can’t directly access or modify other applications or your system Python:
每个 pipx 应用程序都在完全隔离的环境中运行,因此受感染的应用程序无法直接访问或修改其他应用程序或你的系统 Python:
# Isolated security contexts // 隔离的安全上下文
pipx install tool-a # Runs in environment A // 在环境 A 中运行
pipx install tool-b # Runs in environment B // 在环境 B 中运行
# tool-a cannot access tool-b's dependencies, data, or functionality // tool-a 无法访问 tool-b 的依赖项、数据或功能
This isolation also makes security incident response much simpler:
这种隔离也使安全事件响应变得更加简单:
# Security incident response // 安全事件响应
pipx uninstall compromised-tool # Completely removes tool and its environment // 完全移除工具及其环境
pipx install --force-reinstall safe-tool # Clean reinstall if needed // 如有需要,彻底重新安装
pipx list --verbose # Audit all installed applications and their isolation // 审计所有已安装的应用程序及其隔离情况
Final thoughts
最终思考
We compared pip and pipx to help you understand when to use each tool in your Python workflow.
我们对 pip 和 pipx 进行了比较,以帮助你了解在 Python 工作流中何时使用每种工具。
Use pip when you need packages for your Python projects. Install requests, pandas, django, and other libraries with pip because your code needs to import them.
当你需要为 Python 项目安装包时,请使用 pip。用 pip 安装 requests、pandas、django 和其他库,因为你的代码需要导入它们。
Go with pipx for standalone tools you run from the command line. Install black, flake8, jupyter, and similar applications with pipx so they work everywhere without interfering with each other.
对于从命令行运行的独立工具,请使用 pipx。用 pipx 安装 black、flake8、jupyter 和类似的应用程序,这样它们可以在任何地方运行且不会相互干扰。
Python pipx 与 pip 工具的区别
mob649e816209c2 2023-08-14 19:52:00
简介
在 Python 开发中,pip
是常用的包管理工具,而 pipx
是一个用于在独立虚拟环境中安装和运行包的工具,提供了更好的隔离性和灵活性。
pip 工具
pip
是 Python 的官方包管理工具,用于从 Python Package Index(PyPI)下载和安装包。其基本使用命令为:
pip install package_name
其中,package_name
是要安装的包名。
pipx 工具
pipx
是一个在独立虚拟环境中安装和运行包的工具,避免了不同项目之间的包冲突。其主要功能和使用方法如下:
安装 pipx
通过以下命令安装 pipx
:
pip install --user pipx
该命令将 pipx
安装到用户主目录下。
使用 pipx 安装包
使用 pipx
安装包的命令为:
pipx install package_name
若需指定包的版本,可使用 @
符号,例如:
pipx install package_name@1.0.0
运行包
安装完成后,使用以下命令运行包:
pipx run package_name
pip 与 pipx 主要对比
方面 | pip | pipx |
---|---|---|
目的 | 通用的 Python 库和工具安装程序,适合项目特定的依赖项。 | 专门用于安装和运行提供命令行界面(CLI)的 Python 应用程序,确保全局访问且无依赖冲突。 |
隔离性 | 需要手动管理虚拟环境以避免依赖冲突。 | 自动为每个已安装的应用程序创建隔离的虚拟环境,避免依赖冲突。 |
使用场景 | 管理特定项目中的库和依赖项,例如 Web 框架或数据分析库。 | 适合全局使用的独立 CLI 工具,如代码格式化工具(black)或 HTTP 客户端(httpie)。 |
依赖关系共享 | 依赖项在环境中共享,除非显式隔离。 | 每个 CLI 应用程序都有自己的依赖关系,防止重叠或冲突。 |
全局安装 | 全局安装可能导致冲突,除非通过虚拟环境仔细管理。 | 应用程序可以全局使用,而不会干扰其他项目或工具。 |
pip 与 pipx 使用场景
使用场景 | pip | pipx |
---|---|---|
管理项目依赖项 | 非常适合管理单个项目中的库和依赖项,确保可重复性。 | 不适用,更适合全局工具。 |
安装全局 CLI 工具 | 需要手动管理虚拟环境以避免冲突。 | 最适合安装独立的 CLI 工具(如 black、httpie),确保全局访问且无冲突。 |
临时运行 Python 应用程序 | 不推荐,可能导致全局环境污染。 | 适合运行临时 Python 应用程序,不污染全局环境。 |
总结
- 环境隔离性:
pipx
在独立虚拟环境中安装包,避免不同项目之间的包冲突;pip
默认安装到全局环境,可能导致包版本冲突。 - 功能灵活性:
pipx
支持快速运行包而无需安装,适合临时使用;pip
主要用于安装和管理包。 - 适用场景:
pipx
适用于多项目开发和需要隔离环境的场景;pip
适用于单个项目的开发或全局环境管理。
via:
- Pip vs Pipx: Complete Guide to Python Package Management | Better Stack Community
https://betterstack.com/community/comparisons/pip-vs-pipx/ - python pipx 跟 pip工具的区别_mob649e816209c2的技术博客_51CTO博客
https://blog.51cto.com/u_16175490/7080940