Search code examples
pythonpython-os

os.getenv or os.environ don't reach variables in .env file


In my directory, I have a .env file with:

HOST="url_to_host"

I also have my script.py, where:

import os
os.environ["HOST"]

If I run the script in Vscode debug mode, everything works. However, in normal mode, I get a key error, or if I use os.getenv("HOST") I get NoneType.


Solution

  • I added this to my script:

    from dotenv import load_dotenv
    
    load_dotenv()