Uv:Rust 中的 Python 封装
Uv: Python packaging in Rust

原始链接: https://astral.sh/blog/uv

UV 简介:极其快速且模块化的 Python 包解析器和安装程序 UV(发音为“oo-vee”)是一款改变游戏规则的工具,适合那些在管理软件项目时寻求效率、可靠性和易用性的 Python 开发人员。 UV 专为 pip 和 pip-tools 等流行包管理器的直接替代品而设计,提供闪电般的速度,与传统的 Python 打包系统相比,代码执行优化了近 800 倍。 其创新架构利用硬链接和写入时复制技术显着减少资源消耗,同时保留全局模块缓存的优势以实现最佳内存占用。 借助 UV,开发人员可以受益于更简单的工作流程、无缝集成、更低的 CPU 使用率、更少的系统调用、更少的进程创建以及更流畅的整体性能。 Astral 套件的这一强大补充有助于节省开发资源、加快依赖关系分析并成倍提高生产力,从而使团队能够减少等待时间,将更多时间用于创新。 此外,UV 旨在通过增强的依赖性解决来改变 Python 打包环境。 作为我们致力于提供统一且全面的 Python 项目和包管理器的一部分,我们计划开发一个具有传奇色彩的 Python 打包解决方案。 我们近期的计划包括进一步增强 UV 的功能,以提高实用性和价值主张。 因此,无论您是想使用 Trio 部署复杂的科学模拟、利用 Dask 处理复杂的分布式计算任务,还是使用 Ray 简化要求苛刻的机器学习实验,UV 都可以让处理变得更简单,帮助您在一半的时间内创造出更好的科学! 要了解 UV 的工作原理,请访问我们的网站以获取详细文档。 而且,如果您发现自己需要任何 Python 社区成员的帮助,请随时与我们联系! 在 Astral,我们相信每个人都应该能够获得让他们发光发热的优质工具。 这就是为什么我们提供开源、免费且永远可供任何人随时使用的原因。 立即尝试紫外线! 基于上面这段话,UV是如何提高处理Python包依赖的效率、可靠性和易用性的呢? 与 pip 和 pip-tools 等其他包管理器相比,它提供了哪些独特的功能?该工具如何帮助科学家更快地创建更好的研究成果?

总体而言,围绕 Uv 的反馈和兴趣是积极的。 许多人都欣赏它作为更好的类型检查器的好处,包括 flake8 等功能和更有效的代码结构实施。 Ruff 的易用性也受到高度赞扬,尤其是对于初学者而言。 然而,出现了一些有关产品线可持续性和潜在货币化策略的问题。 专家们争论锁定文件或散列机制是否对于保护供应链免受网络威胁至关重要,以及提供满足不同平台依赖性的平台无关解决方案的可行性,特别是通过针对不同平台的不同工件来解决特定于平台的依赖性 和口译员。 虽然大多数 Python 社区都对 Uv 充满热情,但一些评论者建议重点关注提供更清晰的代码结构和有效执行编码约定等方面,而不是创建另一个 Python 打包系统。 最后,一些人对风险投资公司的参与及其对促进盈利目标而非公用事业目标的影响表示担忧,质疑 Uv 是否会优先考虑满足市场需求或履行社会责任。 这一公告引起了人们的兴奋,他们期待着 Poetry 和 Pipenv 等流行打包工具的替代品,渴望探索自动文档生成和动态导入优化等新功能。 一位人士建议将高级类型系统检查机制合并到 Uv 中。 尽管如此,对 Uv 出现的总体期望主要集中在改进打包过程和促进更顺利地过渡到 Python 编程。
相关文章

原文

TL;DR: uv is an extremely fast Python package installer and resolver, written in Rust, and designed as a drop-in replacement for pip and pip-tools workflows.

uv represents a milestone in our pursuit of a "Cargo for Python": a comprehensive Python project and package manager that's fast, reliable, and easy to use.

As part of this release, we're also taking stewardship of Rye, an experimental Python packaging tool from Armin Ronacher. We'll maintain Rye as we expand uv into a unified successor project, to fulfill our shared vision for Python packaging.


At Astral, we build high-performance developer tools for the Python ecosystem. We're best known for Ruff, an extremely fast Python linter and formatter.

Today, we're releasing the next tool in the Astral toolchain: uv, an extremely fast Python package resolver and installer, written in Rust.

0s 1s 2s 3s uv poetry pip-compile pdm 0.60s 1.56s 3.37s 0.01s 0s 2s 4s uv poetry pdm pip-sync 0.99s 1.90s 4.63s 0.06s

Resolving (top) and installing (bottom) the Trio dependencies with a warm cache, to simulate recreating a virtual environment or adding a dependency to an existing project (source).

uv is designed as a drop-in replacement for pip and pip-tools, and is ready for production use today in projects built around those workflows.

Like Ruff, uv's implementation was grounded in our core product principles:

  1. An obsessive focus on performance. In the above benchmarks, uv is 8-10x faster than pip and pip-tools without caching, and 80-115x faster when running with a warm cache (e.g., recreating a virtual environment or updating a dependency). uv uses a global module cache to avoid re-downloading and re-building dependencies, and leverages Copy-on-Write and hardlinks on supported filesystems to minimize disk space usage.
  2. Optimized for adoption. While we have big aspirations for the future of Python packaging, uv's initial release is centered on supporting the pip and pip-tools APIs behind our uv pip interface, making it usable by existing projects with zero configuration. Similarly, uv can be used as "just" a resolver (uv pip compile to lock your dependencies), "just" a virtual environment creator (uv venv), "just" a package installer (uv pip sync), and so on. It's both unified and modular.
  3. A simplified toolchain. uv ships as a single static binary capable of replacing pip, pip-tools, and virtualenv. uv has no direct Python dependency, so you can install it separately from Python itself, avoiding the need to manage pip installations across multiple Python versions (e.g., pip vs. pip3 vs. pip3.7).

While uv will evolve into a complete Python project and package manager (a "Cargo for Python"), the narrower pip-tools scope allows us to solve the low-level problems involved in building such a tool (like package installation) while shipping something immediately useful with minimal barrier to adoption.

You can install uv today via our standalone installers, or from PyPI.

uv supports everything you'd expect from a modern Python packaging tool: editable installs, Git dependencies, URL dependencies, local dependencies, constraint files, source distributions, custom indexes, and more, all designed around drop-in compatibility with your existing tools.

uv supports Linux, Windows, and macOS, and has been tested at-scale against the public PyPI index.

This initial release centers on what we refer to as uv's pip API. It'll be familiar to those that have used pip and pip-tools in the past:

  • Instead of pip install, run uv pip install to install Python dependencies from the command line, a requirements file, or a pyproject.toml.
  • Instead of pip-compile, run uv pip compile to generate a locked requirements.txt.
  • Instead of pip-sync, run uv pip sync to sync a virtual environment with a locked requirements.txt.

By scoping these "lower-level" commands under uv pip, we retain space in the CLI for the more "opinionated" project management API we intend to ship in the future, which will look more like Rye, or Cargo, or Poetry. (Imagine uv run, uv build, and so on.)

uv can also be used as a virtual environment manager via uv venv. It's about 80x faster than python -m venv and 7x faster than virtualenv, with no dependency on Python.

0s 0.02s 0.04s 0.06s 0.08s 74.4ms 24.1ms 4.1ms 0s 0.5s 1s 1.5s 141.4ms 1.54s 18.2ms

Creating a virtual environment, with (top) and without (bottom) seed packages like pip and setuptools (source).

uv's virtual environments are standards-compliant and work interchangeably with other tools — there's no lock-in or customization.

Building our own package management stack from scratch also opened up room for new capabilities. For example:

  • uv supports alternate resolution strategies. By default, uv follows the standard Python dependency resolution strategy of preferring the latest compatible version of each package. But by passing --resolution=lowest, library authors can test their packages against the lowest-compatible version of their dependencies. (This is similar to Go's Minimal version selection.)
  • uv allows for resolutions against arbitrary target Python versions. While pip and pip-tools always resolve against the currently-installed Python version (generating, e.g., a Python 3.12-compatible resolution when running under Python 3.12), uv accepts a --python-version parameter, enabling you to generate, e.g., Python 3.7-compatible resolutions even when running under newer versions.
  • uv allows for dependency “overrides”. uv takes pip's “constraints” concepts a step further via overrides (-o overrides.txt), which allow the user to guide the resolver by overriding the declared dependencies of a package. Overrides give the user an escape hatch for working around erroneous upper bounds and other incorrectly-declared dependencies.

In its current form, uv won't be the right fit for all projects. pip is a mature and stable tool, with extensive support for an extremely wide range of use cases and a focus on compatibility. While uv supports a large fraction of the pip interface, it lacks support for some of its legacy features, like .egg distributions.

Similarly, uv does not yet generate a platform-agnostic lockfile. This matches pip-tools, but differs from Poetry and PDM, making uv a better fit for projects built around the pip and pip-tools workflows.

For those deep in the packaging ecosystem, uv also includes standards-compliant Rust implementations of PEP 440 (version identifiers), PEP 508 (dependency specifiers), PEP 517 (a build-system independent build frontend), PEP 405 (virtual environments), and more.

uv represents an intermediary milestone in our pursuit of a "Cargo for Python": a unified Python package and project manager that is extremely fast, reliable, and easy to use.

Think: a single binary that bootstraps your Python installation and gives you everything you need to be productive with Python, bundling not only pip, pip-tools, and virtualenv, but also pipx, tox, poetry, pyenv, ruff, and more.

Python tooling can be a low-confidence experience: it's a significant amount of work to stand up a new or existing project, and commands fail in confusing ways. In contrast, when working in the Rust ecosystem, you trust the tools to succeed. The Astral toolchain is about bringing Python from a low-confidence to a high-confidence experience.

This vision for Python packaging is not far off from that put forward by Rye, an experimental project and package management tool from Armin Ronacher.

In talking with Armin, it was clear that our visions were closely aligned, but that fulfilling them would require a significant investment in foundational tooling. For example: building such a tool requires an extremely fast, end-to-end integrated, cross-platform resolver and installer. In uv, we've built that foundational tooling.

We saw this as a rare opportunity to team up, and to avoid fragmenting the Python ecosystem. As such, in collaboration with Armin, we're excited to be taking over Rye. Our goal is to evolve uv into a production-ready "Cargo for Python", and to provide a smooth migration path from Rye to uv when the time is right.

Until then, we'll be maintaining Rye, migrating it to use uv under-the-hood, and, more generally, treating it as an experimental testbed for the end-user experience we're building towards.

While merging projects comes with its own challenges, we're committed to building a single, unified tool under the Astral banner, and to supporting existing Rye users as we evolve uv into a suitable and comprehensive successor project.

Following this release, our first priority is to support users as they consider uv, with a focus on improving compatibility, performance, and stability across platforms.

From there, we'll look towards expanding uv into a complete Python project and package manager: a single binary that gives you everything you need to be productive with Python.

We have an ambitious roadmap for uv. But even in its current form, I think it will feel like a very different experience for Python. I hope you'll give it a try.

Finally, we'd like to thank all those that contributed directly or indirectly to the development of uv. Foremost among them are Jacob Finkelman and Matthieu Pizenberg, the maintainers of pubgrub-rs. uv uses PubGrub as its underlying version solver, and we're grateful to Jacob and Matthieu for the work they put into PubGrub in the past, and for the way they've engaged with us as collaborators throughout the project.

We'd also like to thank those projects in the packaging space that've inspired us, especially Cargo, along with Bun, Orogene, and pnpm from the JavaScript ecosystem, and Posy, Monotrail, and Rye from the Python ecosystem. In particular, thanks to Armin Ronacher for collaborating with us on this effort.

Finally, we'd like to thank the maintainers of pip and the members of the PyPA more broadly for all the work they do to make Python packaging possible.

联系我们 contact @ memedata.com