0

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.

1
  • So is it POSTGRES or POSTRES ? You could simply add a print in your config section right after declaring POSTGRES to verify that it is indeed has a value. Commented Jun 4, 2021 at 20:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.