1

I am creating a simple app that connects to an API and stores specific data into a list. In order to connect to the API, I must do BasicAuth with an email and password. Whenever I hardcode my email and password into the Python script, it works perfectly, however whenever I store these values in a .env file and do os.getenv('EMAIL'), I am getting a 401 error. I proceeded to print out the value of EMAIL and PASSWORD when EMAIL = os.getenv('EMAIL') and PASSWORD = os.getenv('PASSWORD') but it is printing None in the console. Here is my code in the Python script:

    load_dotenv()
    EMAIL = os.getenv('EMAIL')
    PASSWORD = os.getenv('PASSWORD')

Here is what my .env file looks like:

    EMAIL = importantemail@mydomain.com  
    PASSWORD = secret_password

The two files are in the same folder but I am thinking that I need to do something like pointing my working directory to the folder that has the script and the .env file. This is my first full-fledged project so I believe that I am missing something obvious.

saloua
  • 2,433
  • 4
  • 27
  • 37

1 Answers1

0

os.getenv() is for system environment variables like HOME, PATH, etc. This function does not read any data from your .env file.

You can use this parser, or parse file by your own with os package.

  • Thanks for the answer, in my case, would it be better if I created system environment variables with the values that I need? – Logan Whitehair Nov 27 '21 at 18:14
  • I think no, system environment variables are used for more general purposes (also, they could be used by different applications/users). Whereas you wanna store application-related information. I would definitely use the env file and parse it. – Alexander Slesarev Nov 27 '21 at 18:21