Skip to content

Running commands in projectsプロジェクト内でのコマンドの実行

When working on a project, it is installed into the virtual environment at .venv. This environment is isolated from the current shell by default, so invocations that require the project, e.g., python -c "import example", will fail. Instead, use uv run to run commands in the project environment:プロジェクトで作業しているときは、.venvに仮想環境がインストールされます。この環境はデフォルトで現在のシェルから隔離されているため、プロジェクトを必要とする呼び出し、例えば、python -c "import example"は失敗します。代わりに、uv runを使用してプロジェクト環境内でコマンドを実行してください:

$ uv run python -c "import example"

When using run, uv will ensure that the project environment is up-to-date before running the given command.runを使用する際、uvは指定されたコマンドを実行する前にプロジェクト環境が最新であることを確認します。

The given command can be provided by the project environment or exist outside of it, e.g.:指定されたコマンドはプロジェクト環境によって提供されるか、環境外に存在することがあります。例えば:

$ # Presuming the project provides `example-cli`
$ uv run example-cli foo

$ # Running a `bash` script that requires the project to be available
$ uv run bash scripts/foo.sh

Requesting additional dependencies追加の依存関係の要求

Additional dependencies or different versions of dependencies can be requested per invocation.追加の依存関係や異なるバージョンの依存関係は、呼び出しごとに要求できます。

The --with option is used to include a dependency for the invocation, e.g., to request a different version of httpx:--withオプションは、呼び出しのために依存関係を含めるために使用されます。例えば、httpxの異なるバージョンを要求するために:

$ uv run --with httpx==0.26.0 python -c "import httpx; print(httpx.__version__)"
0.26.0
$ uv run --with httpx==0.25.0 python -c "import httpx; print(httpx.__version__)"
0.25.0

The requested version will be respected regardless of the project's requirements. For example, even if the project requires httpx==0.24.0, the output above would be the same.要求されたバージョンは、プロジェクトの要件に関係なく尊重されます。例えば、プロジェクトがhttpx==0.24.0を要求していても、上記の出力は同じになります。

Running scriptsスクリプトの実行

Scripts that declare inline metadata are automatically executed in environments isolated from the project. See the scripts guide for more details.インラインメタデータを宣言するスクリプトは、プロジェクトから隔離された環境で自動的に実行されます。詳細については、スクリプトガイドを参照してください。

For example, given a script:例えば、次のようなスクリプトがあるとします:

example.py
# /// script
# dependencies = [
#   "httpx",
# ]
# ///

import httpx

resp = httpx.get("https://peps.python.org/api/peps.json")
data = resp.json()
print([(k, v["title"]) for k, v in data.items()][:10])

The invocation uv run example.py would run isolated from the project with only the given dependencies listed.uv run example.pyの呼び出しは、指定された依存関係のみを持ってプロジェクトから隔離されて実行されます。

Legacy scripts on WindowsWindowsのレガシースクリプト

Support is provided for legacy setuptools scripts. These types of scripts are additional files installed by setuptools in .venv\Scripts.サポートは レガシーsetuptoolsスクリプトに提供されています。 これらのタイプのスクリプトは、setuptoolsによって.venv\Scriptsにインストールされる追加ファイルです。

Currently only legacy scripts with the .ps1, .cmd, and .bat extensions are supported.現在、.ps1.cmd、および.bat拡張子を持つレガシースクリプトのみがサポートされています。

For example, below is an example running a Command Prompt script.例えば、以下はコマンドプロンプトスクリプトを実行する例です。

$ uv run --with nuitka==2.6.7 -- nuitka.cmd --version

In addition, you don't need to specify the extension. uv will automatically look for files ending in .ps1, .cmd, and .bat in that order of execution on your behalf.さらに、拡張子を指定する必要はありません。uvは、自動的に.ps1.cmd、および.batで終わるファイルをその実行順序で探します。

$ uv run --with nuitka==2.6.7 -- nuitka --version

Signal handlingシグナル処理

uv does not cede control of the process to the spawned command in order to provide better error messages on failure. Consequently, uv is responsible for forwarding some signals to the child process the requested command runs in.uvは、失敗時により良いエラーメッセージを提供するために、生成されたコマンドにプロセスの制御を譲渡しません。したがって、uvは要求されたコマンドが実行される子プロセスにいくつかのシグナルを転送する責任があります。

On Unix systems, uv will forward most signals (with the exception of SIGKILL, SIGCHLD, SIGIO, and SIGPOLL) to the child process. Since terminals send SIGINT to the foreground process group on Ctrl-C, uv will only forward a SIGINT to the child process if it is sent more than once or the child process group differs from uv's.Unixシステムでは、uvはほとんどのシグナル(SIGKILL、SIGCHLD、SIGIO、およびSIGPOLLを除く)を子プロセスに転送します。端末はCtrl-CでフォアグラウンドプロセスグループにSIGINTを送信するため、uvは子プロセスグループがuvのものと異なる場合、またはSIGINTが複数回送信された場合にのみ子プロセスにSIGINTを転送します。

On Windows, these concepts do not apply and uv ignores Ctrl-C events, deferring handling to the child process so it can exit cleanly.Windowsでは、これらの概念は適用されず、uvはCtrl-Cイベントを無視し、子プロセスがクリーンに終了できるように処理を遅延させます。