Environment, Python

Reading Environment Variables From .Env File In Python

Reading environment variables in python is very easy using a great package called python-dotenv. Very often you would like to have a separate .env file to store your configurations variable. You can hide the .env file from your developers and keep your private credentials secure using this method.

You can read the key, value pair from .env and adds them to environment variable. It is great for managing app settings during development and in production using 12-factor principles.

Basic App Structure


Simply run the command below to install python-dotenv

pip install -U python-dotenv

Add Variables To .Env File

We will add two very basic variables inside the .env file. Open up the .env and paste the code given below this paragraph.

Loading .Env File

Paste this code into your settings.py file to load variables from .env file.

Accessing Environment Variables

To access environment variables, simply use os.getenv('VARIABLE-KEY'). Here is a complete example showing how to load and use variables from .env file.

You can run the settings.py file to see output of your environment variables in your python console.

That’s it! now you know how to read .env file variables in python. You can use this method in any Python project. I’ve used it with flask and Django project and found it very useful.

For more information, you can check the repo official documentation at https://github.com/theskumar/python-dotenv

You can check my other Python articles here https://robinislam.me/blog/category/python/

Tagged , , , ,

About Robin

A learner who loves technology very much. Dreaming to become a programmer but also enjoying the life.
View all posts by Robin →

8 thoughts on “Reading Environment Variables From .Env File In Python

  1. Is there an easier way to access all the variables in just one line of command?

    Let’s say I have a .env file which contains a huge amount of variables, and it will be too time-consuming if I do this for each variable:

    # Accessing variables.
    status = os.getenv(‘STATUS’)
    secret_key = os.getenv(‘SECRET_KEY’)

    1. Hello Molly,
      Thanks for your comment. Unfortunately, you can only use a key, value pair inside the .env file and this setup is one time so even it’s too big I think it will not take long.

      1. Thanks for all the help Robin!

        In my script, I used this:

        import os
        from dotenv import load_dotenv
        project_folder = os.path.expanduser(‘~/Code/projectName’)
        load_dotenv(os.path.join(project_folder, ‘.env’))

        os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘projectName.settings’)

        and I didn’t have to change anything in settings.py. I was able to access the variables like:

  2. I think you made a little mistake, it worked out for me:

    dotenv_path = join(dirname(__name__), “.env”)

    intead of,

    dotenv_path = join(dirname(__file__), “.env”)

    Never mine, I really enjoy your concise post.

    1. The difference could happen because of the different directory structure. Because dotenv_path = join(dirname(__file__), “.env”) this is still working for me here :p
      There is nothing to mind bro, I’m glad to know you found a way to fix it for you. Thanks a lot for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.