Fred
Fred

Reputation: 583

Run python script with uv with path

I'm using uv as my python project manager. Within the project dir I can run my scripts with uv run my_script.py and all dependencies are resolved.

But how can I call this script if my terminal in on another path? uv run /path/to/my_script.py fails resolving the dependencies.

Upvotes: 2

Views: 331

Answers (1)

Hericks
Hericks

Reputation: 10464

There are multiple options to run the script.

1. Using uv run with the --project parameter.

uv run accepts a dedicated --projects parameter to run the script within the given project directory. From the documentation:

--project project Run the command within the given project directory.

All pyproject.toml, uv.toml, and .python-version files will be discovered by walking up the directory tree from the project root, as will the project’s virtual environment (.venv).

Other command-line arguments (such as relative paths) will be resolved relative to the current working directory.

[...]

2. Using the project virtual environment explicitly.

Alternatively, you could activate the project's virtual environment explicitly.

source /path/to/your/.venv/bin/activate

With the project's virtual environment activated, running your script independently of uv should work just as well.

python3 /path/to/your/script.py

Upvotes: 1

Related Questions