How To Create .ENV File in Python
Env
files allow you to put your environment variables inside a file. You
just create a new file called. env in your project and slap your
variables in there on different lines.
This env is used for storing your all id, password, keys all important credential so no on directly can get your data.
Code
Step 1: Create a virtual environment and make it active
Step 2: pip install python-dotenv
Step 3: Create a .env file
Step 4: Create test.py in the same dir where you created .env file
Now Check Code
test.py
from dotenv import load_dotenv
load_dotenv()
import os
my_site = os.getenv("DOMAIN")
print("Visit My website : {} ".format(my_site))
.env
# a comment that will be ignored.
DOMAIN=https://thepyuniverse.blogspot.com
Now Run test.py
Output:
Visit My website : https://thepyuniverse.blogspot.com
Screenshot :
Wow really very helpful man
ReplyDelete