5

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.

2
  • You are within a project that has an associated virtual environment (.venv)? Commented Jan 27, 2025 at 9:14
  • yes, the project has a working venv in ./.venv Commented Jan 27, 2025 at 11:16

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

I tried uv run --project /path/to/my_script.py uv run --project /path/to my_script.py but only uv run --project /path/to/ /path/to/my_script.py worked. This syntax looks quite silly but better than nothing. Thanks
@Fred --project just indicates that the parameter is used. Afterwards you pass the path to the project. Finally, the path to the script is still required of course. This gives the syntax uv run --project /path/to/project /path/to/script.
OK. Understand. I thought it is like --take-the-path-you-sit-in-as-your-project which would avoid the double path entry.
1

This works for me:

uv run --directory /path/to/main main.py

Comments

1

Assuming your script folder absolute/path/to/ contains the uv environment then:

uv run --script "absolute/path/to/my_script.py"

executes the script with the uv default python executable (e.g. "C:\Users\USER_NAME\AppData\Roaming\uv\python\cpython-3.14.2-windows-x86_64-none\python.exe")

When running

uv run --directory "absolute/path/to/" my_script.py

then the same script is executed with python executable specified in the project uv configuration (e.g. absolute/path/to/.venv/Scripts/python.exe)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.