运行带有依赖项的脚本
Uv: Running a script with dependencies

原始链接: https://docs.astral.sh/uv/guides/scripts/#running-a-script-with-dependencies

## uv run:简化的 Python 脚本执行 `uv run` 通过自动管理依赖和虚拟环境,简化了 Python 脚本的执行。无需手动设置环境,`uv` 会按需创建环境,并可以通过脚本内声明或命令行标志(如 `--with rich`)来指定依赖项。 脚本可以直接运行(`uv run example.py`),接受参数,甚至可以从标准输入读取代码。对于使用 `pyproject.toml` 的项目,`uv run` 会先安装项目;使用 `--no-project` 可以跳过此步骤。 现代 Python 的内联脚本元数据(通过 `uv init --script` 初始化)允许在脚本本身 *内部* 声明依赖项,覆盖项目依赖项。还可以使用 `uv lock --script` 锁定依赖项以实现可重复性,从而创建一个 `.lock` 文件。 `uv` 还支持指定 Python 版本(`--python 3.10`)和软件包索引(`--index`)。在 Windows 上,`.pyw` 脚本使用 `pythonw` 执行。这种方法简化了脚本执行,确保了环境的一致性和易于管理的依赖项。

## uv 包管理器简化脚本依赖管理 `uv` 包管理器提供了一种便捷的方式来运行带有依赖项的 Python 脚本,而无需手动创建虚拟环境。通过在脚本中使用特殊的注释块声明依赖项(符合 PEP 723 标准),`uv run script.py` 会自动将其安装到临时环境中。 此功能对于一次性脚本或工具特别有用,因为你不想污染你的主 Python 环境——正如一位用户所强调的,例如 git 钩子。它简化了协作,只需要简单的 `brew install uv` 指令即可供开发者使用。 值得注意的是,Claude 4 AI 现在可以生成*包含*这些内联依赖项的 Python 脚本,从而进一步简化了流程。用户称赞这为“杀手级功能”,也是转向 `uv` 的关键原因。
相关文章

原文

A Python script is a file intended for standalone execution, e.g., with python <script>.py. Using uv to execute scripts ensures that script dependencies are managed without manually managing environments.

Note

If you are not familiar with Python environments: every Python installation has an environment that packages can be installed in. Typically, creating virtual environments is recommended to isolate packages required by each script. uv automatically manages virtual environments for you and prefers a declarative approach to dependencies.

If your script has no dependencies, you can execute it with uv run:

Similarly, if your script depends on a module in the standard library, there's nothing more to do:

Arguments may be provided to the script:

Additionally, your script can be read directly from stdin:

Or, if your shell supports here-documents:

Note that if you use uv run in a project, i.e., a directory with a pyproject.toml, it will install the current project before running the script. If your script does not depend on the project, use the --no-project flag to skip this:

See the projects guide for more details on working in projects.

When your script requires other packages, they must be installed into the environment that the script runs in. uv prefers to create these environments on-demand instead of using a long-lived virtual environment with manually managed dependencies. This requires explicit declaration of dependencies that are required for the script. Generally, it's recommended to use a project or inline metadata to declare dependencies, but uv supports requesting dependencies per invocation as well.

For example, the following script requires rich.

If executed without specifying a dependency, this script will fail:

Request the dependency using the --with option:

Constraints can be added to the requested dependency if specific versions are needed:

Multiple dependencies can be requested by repeating with --with option.

Note that if uv run is used in a project, these dependencies will be included in addition to the project's dependencies. To opt-out of this behavior, use the --no-project flag.

Python recently added a standard format for inline script metadata. It allows for selecting Python versions and defining dependencies. Use uv init --script to initialize scripts with the inline metadata:

The inline metadata format allows the dependencies for a script to be declared in the script itself.

uv supports adding and updating inline script metadata for you. Use uv add --script to declare the dependencies for the script:

This will add a script section at the top of the script declaring the dependencies using TOML:

uv will automatically create an environment with the dependencies necessary to run the script, e.g.:

Important

When using inline script metadata, even if uv run is used in a project, the project's dependencies will be ignored. The --no-project flag is not required.

uv also respects Python version requirements:

Note

The dependencies field must be provided even if empty.

uv run will search for and use the required Python version. The Python version will download if it is not installed — see the documentation on Python versions for more details.

A shebang can be added to make a script executable without using uv run — this makes it easy to run scripts that are on your PATH or in the current folder.

For example, create a file called greet with the following contents

Ensure that your script is executable, e.g., with chmod +x greet, then run the script:

Declaration of dependencies is also supported in this context, for example:

If you wish to use an alternative package index to resolve dependencies, you can provide the index with the --index option:

This will include the package data in the inline metadata:

If you require authentication to access the package index, then please refer to the package index documentation.

uv supports locking dependencies for PEP 723 scripts using the uv.lock file format. Unlike with projects, scripts must be explicitly locked using uv lock:

Running uv lock --script will create a .lock file adjacent to the script (e.g., example.py.lock).

Once locked, subsequent operations like uv run --script, uv add --script, uv export --script, and uv tree --script will reuse the locked dependencies, updating the lockfile if necessary.

If no such lockfile is present, commands like uv export --script will still function as expected, but will not create a lockfile.

In addition to locking dependencies, uv supports an exclude-newer field in the tool.uv section of inline script metadata to limit uv to only considering distributions released before a specific date. This is useful for improving the reproducibility of your script when run at a later point in time.

The date must be specified as an RFC 3339 timestamp (e.g., 2006-12-02T02:07:43Z).

uv allows arbitrary Python versions to be requested on each script invocation, for example:

See the Python version request documentation for more details on requesting Python versions.

On Windows uv will run your script ending with .pyw extension using pythonw:

Run Result

Similarly, it works with dependencies as well:

Run Result

To learn more about uv run, see the command reference.

Or, read on to learn how to run and install tools with uv.

联系我们 contact @ memedata.com