Skip to main content

Simple script execution with uv - run Python scripts without explicit --project flag

Project description

Simple Script Execution with uvr

uv is a fast, modern Python package installer and resolver, designed as a drop-in replacement for pip and pip-compile.

Unfortunately, uv prioritizes virtual environments within the current directory. This makes it cumbersome to execute scripts located elsewhere, requiring the use of the --project flag.

This script offers a streamlined workaround for running Python scripts via uv, allowing you to use uvr [options] script.py instead of uv run [options] --project <script_path> script.py."

Its primary value lies in its simplicity and immediate usability, providing a quick fix for a pressing pain point.

Installation

To install uvr, use the following command:

uv tool install uvr

or

uv tool install --from git+https://github.com/karnigen/uvr uvr

To upgrade uvr, use the following command:

uv tool upgrade uvr

Usage

Several ways to run your Python scripts with uv:

  1. Using uv run --project <script_path> script.py:

    • This command explicitly tells uv to run the specified Python script (script.py) within the context of the project located at <script_path>.

    • This is useful when your script relies on dependencies defined within a specific project directory.

    • Example:

      uv run [options] --project /path/to/project run_script.py [script_options]
      
  2. Using uvr script.py:

    • This is a more direct way to execute your Python script (run_script.py) using uvr.

    • uvr automatically determines the project directory based on the script path, effectively mimicking the --project flag's behavior.

    • Example:

      uvr [options] [--] run_script.py [script_options]
      
    • Always use -- if the automatic script identification fails or could be ambiguous.

  3. Shebang Usage:

    • Example:

      #!/usr/bin/env -S uvr [options] [--]
      
      # Your Python code here...
      
    • Note on -S flag: The -S flag allows passing multiple arguments to the interpreter. It is optional if your env implementation supports it (most modern systems do). However:

      • Without -S: You can only use #!/usr/bin/env uvr without additional options or parameters
      • With -S: You can pass options like #!/usr/bin/env -S uvr --with dep1 --
      • Some older or minimal Unix-like systems may not support the -S flag in env
    • Always use -- if the automatic script identification fails or could be ambiguous.

  4. Scripts without .py or .pyw extension:

    • Automatic --script option is added if not already present (--script or --gui-script) in options.

    • Otherwise, uv might loop indefinitely.

    • Example: For a foo script:

          #!/usr/bin/env -S uvr [options] [--]
          # Your Python code here...
      

      This will be executed as uv run [options] --script ... if [options] do not already contain --script or --gui-script.

    • Or, to be more explicit, you can include the --script flag directly in the shebang:

      #!/usr/bin/env -S uvr --script
      

      (Use #!/usr/bin/env uvr if -S is not supported, but then you cannot pass additional options)

    • Important Exception for Non-Files: If the identified script_path (the argument immediately following options or --) does not point to an actual file on disk, uvr will not automatically add the --script or --gui-script option. This behavior ensures uvr can correctly pass through commands that are executables within the virtual environment (e.g., uvr black ., uvr pytest), rather than a Python script file.

  5. Debug usage:

    • Example:
      uvr -v [options] [--] run_script.py [script_options]
      uvr -vv [options] [--] run_script.py [script_options]
      

General Rule for Using the -- Separator

The -- argument functions as a standard command-line delimiter. It explicitly separates options intended for uvr (and its underlying uv process) from arguments specifically designated for the Python script being executed.

Arguments appearing before the -- are processed by uvr (uv). Arguments appearing after the -- are passed directly to the invoked Python script.

This explicit separation is crucial for:

  • Preventing Ambiguity: uvr employs a basic heuristic to identify the script path (the first non-hyphenated argument). This can lead to misinterpretation if the script itself accepts options that resemble uvr/uv arguments.

  • Ensuring Precise Argument Passing: By using --, users guarantee that all subsequent arguments are correctly delivered to their script, bypassing uvr's argument parsing logic.

Recommendation: Utilize the -- separator whenever precise control over argument distribution between uvr/uv and the target script is required.

Ctrl+C (SIGINT) Handling

uvr handles KeyboardInterrupt to provide clean CLI behavior when interrupted with Ctrl+C.

  • It suppresses Python traceback noise (unwanted junk output) on user interruption.
  • It exits with status code 130, following the common Unix convention: 128 + 2 (SIGINT is signal number 2).
  • This keeps output clean while still signaling to the parent shell/process that execution was interrupted.

Platform note:

  • On Linux and macOS, 130 matches the common signal-derived convention (128 + SIGINT 2).
  • On Windows, 130 is used intentionally as a consistent, cross-platform interruption code for uvr.

Security Notes

uvr is a convenience wrapper around uv run. It does not add privileges by itself, but it will execute the script/command you pass to it.

Main risks:

  • Running untrusted scripts can execute arbitrary code.
  • Untrusted dependency sources can introduce supply-chain risk.
  • Running as root/Administrator increases impact if something goes wrong.
  • A compromised PATH could resolve a malicious uv binary.

Recommended protections:

  • Run only trusted scripts and trusted dependency sources.
  • Do not run uvr as root/Administrator unless strictly required.
  • In CI, pin dependencies and use a lockfile where possible.
  • Prefer explicit script paths for automation (script.py) over ambiguous invocations.
  • Keep build/runtime environments isolated (virtual environments, containers, CI runners).
  • Ensure your PATH resolves to the expected uv executable.

Scope note:

  • uvr is not a sandbox. It is a command runner helper and should be used with standard secure development practices.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uvr-0.1.26.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uvr-0.1.26-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file uvr-0.1.26.tar.gz.

File metadata

  • Download URL: uvr-0.1.26.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for uvr-0.1.26.tar.gz
Algorithm Hash digest
SHA256 4c55ebf8e3db02c79f2dc231ba00a53780c5f92b2cce729fd24244d1ce71ff4b
MD5 3f3fc9908249e87ffe1ad258b284c961
BLAKE2b-256 5f7ead4216530ae13898ecfcc0cfdc86ca3e7275b80fcbb536cd66884f8d228c

See more details on using hashes here.

File details

Details for the file uvr-0.1.26-py3-none-any.whl.

File metadata

  • Download URL: uvr-0.1.26-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for uvr-0.1.26-py3-none-any.whl
Algorithm Hash digest
SHA256 1dfd16d350e2afd955784a58f743fa8b838c515d53a716697fffd8ab64f47cdf
MD5 05734563377a1cb72b6a8143562b9679
BLAKE2b-256 2b0daf57edadf43693949d6222feee24cd28d9e395bc93f772ee99f1326db907

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page