Forums

os.getenv('X') returns none

I have followed everything on the guide and it still returns none no matter what I do.

Is there anything beyond what is in the guide that I can try?

No. If you follow the guide correctly, it will work.

I have done everything on the guide step by step and it just doesn't seem to be working? What do I do next then?

You must have done something differently, and we have no information about what you have done.

wsgi file:

import os

from dotenv import load_dotenv

project_folder = os.path.expanduser('~/mysite') # adjust as appropriate

load_dotenv(os.path.join(project_folder, '.env'))

.env file:

export DATABASE_PASSWORD=password

testing it:

import os

print(os.getenv('DATABASE_PASSWORD'))

returns None

Where are you running that test? The print statement is not going to output anything from your web app.

I'm running it outside the web app to just try to get it working for now. It is printing 'None'.

If you're running it outside of the web app, it will not have .env loaded since it's being loaded by wsgi the process which runs the web app.

Is there any way I can run and load them outside? For instance when using tasks and I want to perform an automation

Sure, if you use .env file and dotenv library, you can always load them -- the only caveat is to make sure you provide a path that is meaningful for the interpreter, the easiest way would be to use an absolute path to the .env file (e.g. /home/username/my_project/.env).