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.

0

1 Answer 1

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'))
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm. Just tried this with the same result. "none".
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.