-1

Trying to load environment varibles in python module

import os
    
class Config(object):
    port = os.environ.get("PORT") or 5000
    
print(Config.port)
    
$ 5000
Felix Orinda
  • 593
  • 4
  • 20
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 18 '21 at 06:03
  • Help in loading environment variables in python – Felix Orinda Oct 18 '21 at 06:18

1 Answers1

1

This is an easy fix for the problem I just experiences with flask

  • Create virtual environment using pip or pipenv i.e virtualenv env
  • Activate the virtual environment source env/bin/acivate
  • Install 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

In my app.py I have this

import 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)

Output

$ Somevalue
Felix Orinda
  • 593
  • 4
  • 20