question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotenv file not loaded correctly

See original GitHub issue

Checklist

  • 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:closed
  • Created 2 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Jan 12, 2022

@Kludex what do you think about this thread and this message at all?

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 so uvicorn can load the environment variables with prefix UVICORN_ located on that file… And I’m not sure if it’s worth it.

0reactions
Niccolumcommented, Jan 12, 2022

I came up with solution with small changes. I’ll implement it this week, if not someone implement it faster 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found