Trying to load environment varibles in python module
import os
class Config(object):
port = os.environ.get("PORT") or 5000
print(Config.port)
$ 5000
pip
or pipenv
ie virtualenv env
source env/bin/acivate
python-dotenv
module pip install python-dotenv
On top of your python file have something like this
In my .env
file I have a variable named MY_VARIABLE
MY_VARIABLE=Somevalue
app.py
I have thisimport os
from dotenv import load_dotenv
# Loading up the values
load_dotenv()
class Config(object):
port = os.environ.get("MY_VARIABLE") or 5000
print(Config.port)
$ Somevalue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.