0

I have a project that I'm working on in which I need to store sensitive information into an environment file as variables that can later be called in my code. I'm having issues with it working and so I've dumbed it down to the simplest test I can think of.

I have create a test.py file and a var.env file within the same directory. They are the only files in this directory.

Here is my test.py that simply tried to print the value

#test.py
import os
from dotenv import load_dotenv

print(os.getenv('PROJECT'))

Here is environment file saved as var.env

#.env test file
PROJECT='newproject1234'

When I run test.py I get a response of "none". I know I've gotta be missing something simple here. Any help is appreciated.

1 Answers1

2

You need to call load_dotenv first.

#test.py
import os
from dotenv import load_dotenv

load_dotenv('var.env')

print(os.getenv('PROJECT'))
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Hmm. Just tried this with the same result. "none". – cypherlock1010 Nov 17 '22 at 16:16
  • Updated; without an argument, `load_dotenv` assumes it is loading a file named `.env`. For a different file, you need to provide the file name. – chepner Nov 17 '22 at 16:17
  • AH-HA!! Thank you so much! So by default it just assumes it's looking for .env. If I want to name the .env file anything different I need to specify as an argument within load_dotenv(). Thank you I was banging my head against the wall lol. – cypherlock1010 Nov 17 '22 at 16:24