[Python] How to import from config file in Flask?

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have followed the layout of my Flask project from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world.

    I have the following structure:

    app/
    __init__.py
    views.py
    forms.py
    myFile.py
    run.py
    config.py


    In views.py, forms.py I am able to use

    from config import basedir


    However I cannot use that in myFile.py

    I added

    import Flask


    and when I modify it, the Flask web server restarts, but it doesn't say found changes in app/myFile.py restarting it just restarts.

    What do I need to do to be able to use

    from config import basedir


    in my python file. I don't see anything special being done in __init__.py for forms.py.

    EDIT: This is my __init__.py file:

    from flask import Flask
    from config import basedir

    app = Flask(__name__)
    app.config.from_object('config')
    from app import views

    Continue reading...

Compartilhe esta Página