python: uv 一个好用的 python 包管理工具

据说是一个很快的包管理工具
更新于: 2025-07-15 11:01:12

安装

# linux + macos
curl -LsSf https://astral.sh/uv/install.sh | sh
# 或者
brew install uv

# 直接安装
wget https://github.com/astral-sh/uv/releases/download/0.7.13/uv-x86_64-apple-darwin.tar.gz
tar zxf uv-x86_64-apple-darwin.tar.gz
cd uv-x86_64-apple-darwin && ins uv && ins uvx

# windows

三步曲

cd /Users/aric/aric-notes/uv-notes
# 初始化
uv init .
# 添国 .env 到 .gitignore
# 添加包
uv add requests
# 运行程序
uv run app/main.py

安装多版本的 python

代替 pyenv ?

  • uv python install: Install Python versions.
  • uv python list: View available Python versions.
  • uv python find: Find an installed Python version.
  • uv python pin: Pin the current project to use a specific Python version.
  • uv python uninstall: Uninstall a Python version.
uv python install 3.10
uv python install 3.12

# https://docs.astral.sh/uv/concepts/python-versions/#installing-a-python-version
$ p uv python install 3.10
Installed Python 3.10.18 in 14.16s
 + cpython-3.10.18-macos-x86_64-none

# list local
uv python list --only-installed

常用命令

  • uv init: Create a new Python project.
  • uv add: Add a dependency to the project.
  • uv remove: Remove a dependency from the project.
  • uv sync: Sync the project's dependencies with the environment.
  • uv lock: Create a lockfile for the project's dependencies.
  • uv run: Run a command in the project environment.
  • uv tree: View the dependency tree for the project.
  • uv build: Build the project into distribution archives.
  • uv publish: Publish the project to a package index.

Managing packages in an environment (replacing pip and pipdeptree):

  • uv pip install: Install packages into the current environment.
  • uv pip show: Show details about an installed package.
  • uv pip freeze: List installed packages and their versions.
  • uv pip check: Check that the current environment has compatible packages.
  • uv pip list: List installed packages.
  • uv pip uninstall: Uninstall packages.
  • uv pip tree: View the dependency tree for the environment.

发布包到 pypi 上

# 设置TOKEN,即可以正常 publish
export UV_PUBLISH_TOKEN=$PYPY_TOKEN

私有包

[[tool.uv.index]]
name = "testpypi"
url = "https://test.pypi.org/simple/"
publish-url = "https://test.pypi.org/legacy/"
explicit = true

同步 deps

添加 requirements.txt 里的依赖,参考这里: https://stackoverflow.com/questions/79344035/how-to-add-requirements-txt-to-uv-environment

uv add -r requirements.txt