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.
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:
--projectproject Run the command within the given project directory.All
pyproject.toml,uv.toml, and.python-versionfiles 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With