1

Using load_dotenv to read python variables from a .env file. Building a python package using the setup module and packaging it into a command get_values

Cases when code runs successfully

1- Codebase runs and I can read the environment values only if the command 'get_values' is running from the same directory as codebase.

2- Codebase runs as a standalone Python program

Cases, when code doesn't, runs successfully

1- After building the package, I run the commands 'get_values' the code fails to read the environment variables and get the string value as NoneType

Current Codebase hierarchy:

enter image description here .env file

OIDC_CLIENT_ID='xxxxxxxxxxx'

Code to read environment file

 basepath = Path()
 basedir = str(basepath.cwd())
 envars = basepath.cwd() / 'config.env'
 load_dotenv(envars)

Reading the environment variable

  print(os.getenv('OIDC_CLIENT_ID'))
PiaklA
  • 495
  • 2
  • 7
  • 21
  • what does the output of `import sys; print(sys.path)` say in the failing case? – Leshawn Rice May 11 '22 at 19:05
  • In either cases, getting bunch of *.egg files ['/Library/Frameworks/Python.framework/Versions/3.7/bin', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/aaaaa-1.0-py3.7.egg', – PiaklA May 11 '22 at 19:17
  • Does this answer your question? [How to read a (static) file from inside a Python package?](https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package) – Sören May 11 '22 at 19:37

1 Answers1

0

Make sure your current working directory contains a 'config.env' file in the cases where it's not working.

Sören
  • 1,803
  • 2
  • 16
  • 23