dotenv file not loaded correctly
See original GitHub issueChecklist
- The bug is reproducible against the latest release or
master
. - There are no similar issues or pull requests to fix it yet.
Describe the bug
Uvicorn does not load the dot env file as expected when running it via over the command line or via uvicorn.run
.
Steps to reproduce the bug
File bug.py
:
import uvicorn
if __name__ == '__main__':
uvicorn.run('bug:app', host='localhost', env_file='.env')
# Uvicorn's example app:
async def app(scope, receive, send):
assert scope['type'] == 'http'
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})
await send({
'type': 'http.response.body',
'body': b'Hello, world!',
})
File .env
:
UVICORN_PORT=8081
PORT=8081
Shell:
$ uvicorn bug:app --env-file .env
or
$ python bug.py
Output (both times):
/venv/bin/python3.10 /bug.py
INFO: Loading environment from '.env'
INFO: Started server process [11355]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
Expected behavior
Server running on port 8081.
Actual behavior
Server running on port 8000:
INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
Debugging material
No response
Environment
uvicorn --version
Running uvicorn 0.16.0 with CPython 3.10.0 on Linux
- Run commands:
uvicorn bug:app --env-file .env
python bug.py
Additional context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
dotenv file is not loading environment variables
env file relative to the current working directory from where the application was launched. You can create this path like this: const path...
Read more >5 reasons why your .env environment variables don't work
1. Your framework doesn't automatically load .env files. · 2. You added or otherwise updated your .env file after starting your server. ·...
Read more >Dotenv tutorial - loading environment variables in ...
Dotenv tutorial shows how to load environment variables in JavaScript using the dotenv module.
Read more >dotenv-cli
This will load the variables from the .env file in the current working directory and then ... E.g. the following command without dotenv-cli:....
Read more >Set up and test a .env file in Node
The dotenv package enables loading of a .env file in a Node.js project, which serves as a central ... Please do not commit...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t think this is a bug. This works as intended, even if it doesn’t work with the
UVICORN_
environment variables.I guess the question now is if we want to change the behavior of
env_file
souvicorn
can load the environment variables with prefixUVICORN_
located on that file… And I’m not sure if it’s worth it.I came up with solution with small changes. I’ll implement it this week, if not someone implement it faster 😃