Loading...

Python os.environ.__getitem__ raise KeyError from None

Last updated: Jul 21st 2024

The error comes up because the environment variables are not available on the processor script.  To visualise this, you could simply output the os.environ and see if your variable appears there.

    print(os.environ)

If it doesn't appear, it means that the variable is not loaded on the script and therefore throws the error 'os.environ.__getitem__ raise KeyError from None'

Assuming that you have the variables (key-value pairs) defined in the .evn file, to import them on your script while testing on your LOCAL machine, you could use the [dotenv library][1].

First install dependency:

    pip install python-dotenv

Within the script:

    from dotenv import load_dotenv
    load_dotenv()
    print(os.environ['FOO'])


  [1]: https://pypi.org/project/python-dotenv/

SYED ABDUL QUADER Published on 02 Nov 2021
Edit
Comments
Guest User