Pixi 跨语言包管理¶
一句话说明: Pixi 是基于 conda 生态、用 Rust 编写的跨语言包管理器,兼具 conda 的包丰富性与 cargo/npm 般的速度和现代体验。
为什么要学¶
- 跨语言统一管理 — Python、R、C++、Rust、Node.js 等语言的依赖统一用一个工具管理
- 极快的解析速度 — Rust 实现 + rattler 求解器,比 conda/mamba 更快
- 项目级锁文件 —
pixi.lock精确锁定所有依赖版本,保证可复现性 - 现代化工作流 — 类似
cargo的 task 系统、环境管理、多平台支持 - 兼容 conda-forge — 直接使用 conda-forge 的 10 万+ 包,也支持 PyPI 包
核心概念详解¶
核心文件¶
| 文件 | 作用 | 类比 |
|---|---|---|
pixi.toml | 项目配置(依赖、任务、环境) | Cargo.toml / package.json |
pixi.lock | 精确锁定的依赖树 | Cargo.lock / package-lock.json |
.pixi/ | 本地环境目录(不提交) | node_modules/ / .venv/ |
与其他工具对比¶
| 特性 | Pixi | Conda | Mamba | Poetry |
|---|---|---|---|---|
| 语言 | Rust | Python | C++ | Python |
| 解析速度 | 极快 | 慢 | 快 | 中等 |
| 锁文件 | 原生 | 无 | 无 | 有 |
| 跨语言 | 全面 | 全面 | 全面 | 仅 Python |
| Task 系统 | 内置 | 无 | 无 | 无 |
| PyPI 支持 | 有 | 有限 | 有限 | 原生 |
| 全局工具 | pixi global | 基础环境 | — | — |
环境(Environment)¶
Pixi 支持多环境定义,一个项目中可以有 default、test、docs 等不同环境,各自有独立依赖。
安装与配置¶
安装 Pixi¶
# Linux/macOS
curl -fsSL https://pixi.sh/install.sh | bash
# Windows (PowerShell)
iwr -useb https://pixi.sh/install.ps1 | iex
# Homebrew
brew install pixi
配置 Shell 补全¶
# Bash
echo 'eval "$(pixi completion --shell bash)"' >> ~/.bashrc
# Zsh
echo 'eval "$(pixi completion --shell zsh)"' >> ~/.zshrc
# Fish
pixi completion --shell fish > ~/.config/fish/completions/pixi.fish
验证安装¶
快速上手¶
初始化项目¶
生成的 pixi.toml:
[project]
name = "my-project"
version = "0.1.0"
channels = ["conda-forge"]
platforms = ["linux-64", "osx-arm64", "win-64"]
[dependencies]
添加依赖¶
# 添加 conda 包
pixi add python numpy pandas
# 添加指定版本
pixi add "python>=3.11,<3.13"
# 添加 PyPI 包
pixi add --pypi requests flask
# 添加开发依赖
pixi add --feature test pytest pytest-cov
运行命令¶
# 在 pixi 环境中运行
pixi run python script.py
# 进入 shell
pixi shell
# 定义并运行 task
pixi task add start "python main.py"
pixi run start
进阶用法¶
1. 多环境管理¶
[project]
name = "ml-project"
channels = ["conda-forge"]
platforms = ["linux-64", "osx-arm64"]
[dependencies]
python = ">=3.11"
numpy = "*"
pandas = "*"
[feature.cuda]
platforms = ["linux-64"]
dependencies = { pytorch = ">=2.0", cuda-toolkit = "12.*" }
[feature.test]
dependencies = { pytest = "*", pytest-cov = "*" }
[feature.docs]
dependencies = { sphinx = "*", myst-parser = "*" }
[environments]
default = { solve-group = "default" }
cuda = { features = ["cuda"], solve-group = "default" }
test = { features = ["test"], solve-group = "default" }
docs = { features = ["docs"] }
2. Task 系统¶
[tasks]
start = "python main.py"
test = "pytest tests/ -v"
lint = "ruff check ."
format = "ruff format ."
clean = { cmd = "rm -rf dist/ build/", description = "清理构建产物" }
# 依赖链
build = { cmd = "python -m build", depends-on = ["lint", "test"] }
# 环境特定 task
[feature.test.tasks]
coverage = "pytest --cov=src --cov-report=html"
3. 全局工具安装¶
# 全局安装命令行工具
pixi global install ruff
pixi global install jupyter
pixi global install git
# 列出全局安装
pixi global list
4. 多平台支持¶
[project]
platforms = ["linux-64", "osx-arm64", "win-64"]
# 平台特定依赖
[target.linux-64.dependencies]
cuda-toolkit = "12.*"
[target.osx-arm64.dependencies]
mlx = "*"
5. 与 Docker 集成¶
FROM ghcr.io/prefix-dev/pixi:latest
WORKDIR /app
COPY pixi.toml pixi.lock ./
RUN pixi install
COPY . .
CMD ["pixi", "run", "start"]
常见问题与排错¶
Q: 安装包很慢¶
Q: 与已有 conda 环境冲突¶
Pixi 使用独立的 .pixi/ 目录,不影响系统 conda。两者可共存。
Q: pixi.lock 冲突如何解决¶
Q: 如何安装本地 Python 包(可编辑模式)¶
Q: pixi shell 中 PATH 不对¶
确保 shell 集成正确安装:
Q: 某个包只在 conda 或只在 PyPI 有¶
参考资源¶
- 官方文档:https://pixi.sh/latest/
- GitHub 仓库:https://github.com/prefix-dev/pixi
- 配置参考:https://pixi.sh/latest/reference/pixi_manifest/
- conda-forge 包搜索:https://prefix.dev
- 迁移指南(从 conda):https://pixi.sh/latest/switching_from/conda/
- Pixi 博客:https://prefix.dev/blog