pythonuv

UV Python Packing: How to set environment variables in virtual envrionments


How do I set environment variables in virtual environment creating by UV? I try setting it in .venv/scripts/activate_this.py and it doesn't work.


Solution

  • You can tell the uv run command to load environment variables from a file, either by using the --env-file option:

    uv run --env-file=.env myscript.py
    

    Or by setting the UV_ENV_FILE environment variable:

    export UV_ENV_FILE=.env
    uv run myscript.py
    

    You will find more details in the documentation.