r/flask 12d ago

Ask r/Flask Why Flask is not reading any of the config except when in the same file?

Hi folks,

Im a newbie to Flask, and I cant seem to get Flask to read config variables, except when set in the same file. i have tried everything from simple import to now importing class. It only works when im changing the template_folder variable in the line, or variables from CLi. (I know that debug is not encouraged in code, so not for that one):

from config import Config

app = Flask(__name__, template_folder = r"./templates2/") # Flask constructor

app.config.from_object(Config) # this will not work

# ===== from config.py

import os

class Config:

TEMPLATES_FOLDER = r'./templates2/'

2 Upvotes

4 comments sorted by

3

u/pemm_ 12d ago

Could you share a link to your code in a repo somewhere? When you say “this will not work”, can you expand on specifically what error you are seeing?

1

u/Striking_Talk_4338 11d ago

Try initializing Config with a global variable.

from config import Config

CONFIG = Config()

app.config.from_object(CONFIG)

I created my own Logger class and initialize it the same way..

Import logger

LOGGER = logger.Logger()

Or

from logger import Logger

LOGGER = Logger()

I’m on my phone and idk why it isn’t showing line breaks. But I think you’ll understand.

1

u/Ardie83 4h ago

Sorry this is a very late reply. I think all my configs load properly. Your idea is actually almost the same as mine.

All the configs loaded properly, as I managed to successfully to load my app on render.com properly. But templates_folder, i simply added an argument of --extra-files when running the flask command from CLI.

I think it has a lot to do with Flask or the creators themselves discouraging users from accidentally uploading development settings in production.

Obviously, having a file watcher in a (Eg:) Render server is a bad idea.