Add --env-file flag to the Python CLI

Not every problem needs a python implementation to fix.

Using a shell script is a tried and trusted method to wrap a command for a specific purpose with it’s required context - environment, current-directory, command line options.

Can you give us at least one example?

Creating a PEP is not rocket science.

But note that it’s not the first step. The first step is a thread like this, and finding a core dev who supports the idea enough to sponsor the PEP. I haven’t seen any volunteers.

Alternative: use dotenv inside a PYTHONSTARTUP script.

Docs on that env var: 1. Command line and environment — Python 3.14.5 documentation

1 Like
$ cat .env
FOO = bar baz
$ . ./env
bash: FOO: command not found
$ python -c 'import dotenv; print(dotenv.dotenv_values()["FOO"])'
bar baz

I thought I remembered once constructing an example where quotes were removed by one but treated literally by the other, but dotenv also ignores whitespace in ways that the shell does not.

1 Like

this code dont work

I solved this problem using direnv, is not specific to python, but it works well.

1 Like

I found more easy and clear way to do it

 (set -a && source .env && set +a; python main.py)
1 Like

As an additional note, I think letting the OS handle the env files is also a good way to avoid getting into the “.env files should (not) override shell environment variables” by simply delegating that responsibility to the shell.

The SC once mentioned that while there are many good ideas, they need to be very picky into what they include in the standard library as they would need to maintain it many years down the road

I would suggest sourcing it from your shell (you only need to do it once), or creating an alias :slight_smile:

2 Likes