Python Mini Series: How to load environment variable files in python (works for windows and linux)

Welcome to the python mini-series. In the mini-series, I will cover some small pieces of python which I found useful in the past. Maybe this is also useful for you.

In this article, I will explain how to use environment variable files and how to load those files directly in your python code.

Setting environment variables works differently in Unix based systems than in Windows systems. Therefore it is nice to have an option on how to access and load environment variable files that works across OS.

The article contains two sections:

  1. How to use environment variable files?
  2. How to load environment variable files directly via python?

How to use environment variable files?

Environment variable files – Unix based/Linux

In most shell/bash you have the ‘.’ or ‘source’ command. This command reads and executes commands from the filename in the current shell environment.

To manually set an environment variable you would execute the command export VARNAME=value (depending on the shell/bash). If you need to set many variables and repeatedly then it might be better and more efficient to define this in a file.

The typical naming convention is: .env
The dot before the filename makes it a hidden file in Linux. That being said you can name the files however you like..

The syntax is quite simple. Create a new file and enter the variable with the same command you would use in the console.

To put comments into the file simply add a hash sign => #. Everything behind this in the line will be ignored.

Here an example file .env_test:

#.env_test file
export PASSWORD='Testpassword'
export TESTVAR='This is a test variable'

To load the content of the file simply execute: source .env_test

Echo the variable you set or execute printenv to validate if it worked.


In your python program, you can then access the variables as you are used via os.getenv(‘varname’).

Environment variable files – Windows

In Windows, you do not have the source command available. In order to find an equivalent I had to search a bit.

To manually set an environment variable you would execute the command set VARNAME=value.

In order to set variables in a file you need to create a bat file. This is an executable file in Windows. You can simply create a new file and name it env_test.bat. Confirm the message that will pop up asking if you really want to change the filetype.

Then right-click on the file and edit it in any text editor. Now write per line the same command you would use manually.

Here my example env_test.bat file:

set PASSWORD=’This is a testpassword’
set TESTVAR=’This is a testvariable’

To load the file go to the command line.
Navigate to the folder where the file is located and enter:


You will see in the command line that the set command according to your definition will be executed.

To validate if the command worked execute set without parameters and check if your variables are in the list.

In your python program, you can then access the variables as you are used via os.getenv(‘varname’).

How to load environment variable files directly via python?

In some cases, you do not want to load variables in the above-mentioned way. The files are also quite different in terms of syntax and if you would like to re-use them on another OS you would have issues accessing them. So to avoid this you can simply use the package python-dotenv to load an environment variable file directly into your program.

Here is how to do it:

First, create a environment variable file. You can use the following syntax:


Alternatively, you can also use write the file with the export statement. This statement is simply ignored by the load_dotenv function, however it means you can still source the file under Linux. This is usually my preferred variant.

Second, install the package in your python environment using your python package manager. If you use PIP run the the command: pip install python-dotenv

Third, read the file in your code via the package function load_dotenv. Example code below:

#import os library
import os
#import load_dotenv function
from dotenv import load_dotenv
#import Path class
from pathlib import Path
#set path to .env file
env_path = Path('.\\.env_test')
#load the file
#access the now loaded environment variables
password = os.getenv('PASSWORD')
testvar = os.getenv('TESTVAR')
#print values
print('PASSWORD: '+password)
print('TESTVAR: '+testvar)

Here the breakdown of the code:

  1. import os <= import the python package os. It contains Miscellaneous operating system interfaces. The official python documentation can be found here . The package provides a portable way to access operating system dependent functionality. For example, if you want to open a file, or navigate to a path.
  2. from dotenv import load_dotenv <= import the function load_dotenv.
  3. from pathlib import Path <= import Path class, I used this to set the path to the env file and pass it to the function
  4. env_path = Path(‘..env_test’) <= Get a Path instance pointing to the env_test file
  5. load_dotenv(dotenv_path=env_path) <= load the file
  6. password = os.getenv(‘PASSWORD’) <= access the variables as usual

I hope you found the article useful. There are many ways to set and access environment variables. Let me know the most elegant way you use.


Jan 2nd 22

No one left a comment yet. Be the first!

Leave a comment

We'll never share your email with anyone else.
Slide to submit