Skip to content

Instantly share code, notes, and snippets.

@pdrok
Created May 12, 2022 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdrok/464653ffeafe9dd379249cea6795b828 to your computer and use it in GitHub Desktop.
Save pdrok/464653ffeafe9dd379249cea6795b828 to your computer and use it in GitHub Desktop.
use environment variables in python

Install pip install python-dotenv

URL: https://pypi.org/project/python-dotenv/

Create a file .env with the environment variable you will need ex:

MESSAGE_ENV="Hello from env file"

Create a file app.py with the following content:

import os
from dotenv import load_dotenv

load_dotenv()

message = os.environ["MESSAGE_ENV"].strip()

print(message)

run the file app.py : python app.py

$ python app.py
Hello from env file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment