-
Notifications
You must be signed in to change notification settings - Fork 36
Description
Hello,
Summary
I'm having a bug when using VSCode's terminal to start a flask application because my .env file has a environment variable that starts with dashes.
The problem
When editing my .env file the Python Environment extensions prompted me to use the python.terminal.useEnvFile setting to use the .env files to update my shell's environment, sure, why not? I activated it.
I'm building a flask application with gunicorn and needed to pass configuration flags for gunicorn using the GUNICORN_CMD_ARGS flag. I got an error when trying to run the project with uv:
> uv run --env-file .env gunicorn example_api.main:app
access_log_format = %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
accesslog = None
backlog = 2048
bind = ['ind=0.0.0.0:8080'] <------
ca_certs = None
capture_output = False
...As you can see, the bind parameter is wrong, even though my .env file looks like this:
GUNICORN_CMD_ARGS="--bind=0.0.0.0:8080 --workers=1 --forwarded-allow-ips='*' --print-config"
I started thinking it was an issue with uv run when using with its --env-file flag, and even opened an issue for them (astral-sh/uv#16107), because it worked when passing the env variable inline:
> GUNICORN_CMD_ARGS="--bind=0.0.0.0:8080 --workers=1 --forwarded-allow-ips='*' --print-config" uv run --env-file .env gunicorn example_api.main:app
access_log_format = %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
accesslog = None
backlog = 2048
bind = ['0.0.0.0:8080'] <-----
ca_certs = None
capture_output = False
...I tried the same commands using another terminal outside of VSCode and it worked as expected, which lead me to remember I had activated the setting some hours before.
The extension was incorrectly parsing the .env file when using a environment variable that starts with dashes.
Another question
The extension is overriding my environment variables even though i'm using the --env-file in the uv run command. Is this the intended behaviour?
Reproducible example
- Activate
python.terminal.useEnvFilefirst - Save the following
.envfile inside VSCode so the extension notices it changed
GUNICORN_CMD_ARGS="--bind=0.0.0.0:8080 --workers=1 --forwarded-allow-ips='*' --print-config"example_app.py
from flask import Flask
app = Flask(__name__)
@app.get("/")
def main():
return "ok"pyproject.toml
[project]
name = "example_app"
version = "0.0.1"
requires-python = ">=3.12"
dependencies = ["flask", "gunicorn"]Run inside a VSCode terminal with python.terminal.useEnvFile activated:
> uv install
> uv run --env-file .env gunicorn example_app:appInformation:
uv --version: 0.8.22gunicorn --version: 23.0.0python3 --version: 3.12.8echo $SHELL: zsh 5.8.1- ubuntu 22.04
code --version: 1.104.2- Python Environments: 1.8.0