dotEnv is a lightweight package that automatically loads environment variables from a .env file into the process. To use dotenv in your project, first run this command on terminal:
pip install python-dotenv
To use it in your project, follow these insturactions:
Create .env file in your project
Open it and sign variables to it
# .env file
TOKEN = "This is a token"
PREFIX = "prefix is !"
Open the file where you want to use .env variables
import os
from dotenv import load_dotenv
load_dotenv()
# those are example functions
print(os.get_env("TOKEN")) # This is a token
print(os.get_env("PREFIX")) # prefix is !
Important!
When using git, don't forget to add .env to .gitignore. If its private repositry, you don't need it.
Top comments (1)
Thank you for posting a very important issue in programming using Python. I just want to highlight that on many hosting server, they provide us a place to create environment variables because we cannot push .env file to GitHub that will publicly expose all our confidential information such as database uri, database password, secret key... So the use of python-dotenv is to load our confidential information locally and on the server. However, we need to create the same environment variables locally and on the hosting server.