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: example.pyprint("Hello world") $ uv run example.py Hello world Similarly, if your script depends on a module in the standard library, there's nothing more to do: example.pyimport os print(os.path.expanduser("~")) $ uv run example.py /Users/astral Arguments may be provided to the script: example.pyimport sys print(" ".join(sys.argv[1:])) $ uv run example.py test test $ uv run example.py hello world! hello world! Additionally, your script can be read directly from stdin: $ echo 'print("hello world!")' | uv run - Or, if your shell supports here-documents: uv run - <<EOF print("hello world!") EOF 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: $ # Note: the `--no-project` flag must be provided _before_ the script name. $ uv run --no-project example.py 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 o...
First seen: 2025-07-22 01:41
Last seen: 2025-07-22 07:45