How to save confidential data in environment variables with dotenv

best practice is to never have passwords and alike in clear text in Python scripts - here is the solution I use

21 Jul 2022 starting note.

Library resources
Documentation ---

Python-dotenv reads key-value pairs from a .env file and can set them as environment variables.

What this means is you can create a file called .env in your project(s) and store your confidential variables like passwords, tokens, access keys, IDs and anything you wish in it. You can then retrieve them from your Python scripts, so as to not display them in clear text there.

Originally I did not see the point, as it felt it did not matter if my passwords are in clear text in my script, or in clear text in a different file in my folder.

It clicked obviously when I started to share my code & scripts - first with Github (workin with others, or archiving code there) then sharing my notes publicly here.

See many examples, eg in Nic Note: Library: tmdbapis | fetching movie data where you will see tmdb_api_key = os.getenv("TMDB_API_KEY").

IMPORTANT: when using Git - like Github - make sure to add .env in .gitignore so your file with all your confidential data doesn't end up exposed in your Github repo!


install (ideally in virtual environment):

pip3 install python-dotenv  


create a .env file at the root of your project folder.
no file name, just .env.
for example you can see it here as the first file at the root of the project folder I'm currently writing this in:

├── .env <------------------------
├── .gitignore
├── notes
├── output
└── venv

The content of this .env file should be as follows:


for example for my TMDB scripts, I have:


in your scripts just add then:

from dotenv import load_dotenv

I use dotenv so much that the above snippet is part of my Python boilerplate which you can find here: Nic Note: My Python scripts boilerplate

and then fetch the needed password/token/secret with:

my_password = os.getenv(key_from_.env_file)

for example:

tmdb_api_key = os.getenv("TMDB_API_KEY")

01 Mar 2023

Using dotenv in Python scripts vs hardcoded secrets adds only 10mns at run time.