DEV Community

El Bruno
El Bruno

Posted on β€’ Originally published at elbruno.com on

#Python 🐍- Working with Environment Variables and python-dotnenv

Hi !

The Microsoft Cloud Advocates team is great and, hey everyday I learn something new. A couple of days ago we were talking about working with Environment Variables and Virtual Environments.

I wrote several times about working with Virtual Envs, like

And, I even solve this problem editing the activate and deactivate scripts a long time ago.

Today, I learn that we have the chance to use an amazing library to work with Environment Values: python-dotenv.

Note: for this specific scenario, we are going to define and load env values in a file named [.env].

Check the documentation of the package for a complete reference. Here are 3 steps to show how easy is to use.

We start installing the package

pip install python-dotenv

I created a new python file to test [demo.py] and in the same folder a file to store environment variables [.env].

file structure for venv load vars demo

A couple of code lines will get the values from the env file and ... done !

import os

load_dotenv()
print(f'Foo value from .env: {os.getenv("FOO")}')
print(f'Foo value from .env: {os.environ.get("FOO")}')

Super easy to use !

Continue reading #Python 🐍- Working with Environment Variables and python-dotnenv

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay