Philippe Haumesser
Philippe Haumesser

Reputation: 647

config.py did not send back DATABASE_URL

my config.py:

"""Flask config."""
import os
from dotenv import dotenv_values

basedir = os.path.abspath(os.path.dirname(__file__))

configuration = dotenv_values(".env")

class Config(object):
    DEBUG = False
    TESTING = False
    CSRF_ENABLED = True
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')

class ProductionConfig(Config):
    DEBUG = False

I'm trying to connect to DB with another py file:

import psycopg2
import xlrd
from config import Config


POSTGRES = Config.SQLALCHEMY_DATABASE_URI
connection = psycopg2.connect(POSTGRES)
cursor = connection.cursor()

But POSTRES is None

my .env file is in same directory like this:

FLASK_APP=wsgi.py
FLASK_ENV=development
SECRET_KEY=randomstringofcharacters
DATABASE_URL='postgresql://xxx:[email protected]:5432/xxxxx'

All my files are in same directory.

Upvotes: 0

Views: 87

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

dotenv_values() doesn't side-effect the local environment. You need load_dotenv(), which does.

See https://pypi.org/project/python-dotenv/#load-configuration-without-altering-the-environment

Upvotes: 1

Related Questions