0

There are a ton of these posts trying to sort out imports, but I am unable to figure out exactly what the problem is here based on some of those previous solutions. Below is the folder structure that I have now:

Flask Application Structure

ContactForm/
    app/
      app.py
    home/
      routes.py
    templates/
      index.html

    config.py
    extensions.py
    models.py

app.py

from flask import Flask

from extensions import db
from home.routes import home


def create_app():
    app = Flask(__name__)

    # Check the configuration settings on start up
    if app.config["ENV"] == "production":
        app.config.from_object("config.ProductionConfig")
    elif app.config["ENV"] == "development":
        app.config.from_object("config.DevelopmentConfig")
    elif app.config["ENV"] == "testing":
        app.config.from_object("config.TestingConfig")

    # Initialize any extensions
    db.init_app(app)

    # Register the different routes
    app.register_blueprint(home)

    # Set up the application context
    with app.app_context():
        db.create_all()

    return app

First, I checked what my PYTHONPATH looks like because I know this is a common problem and it is set to PYTHONPATH=C:\Users\den\code\FlaskApps\ContactForm. So Python should be able to see all of the files within the root project directory. I am thinking that perhaps the issue might be that I am running flask run from within app.py, and as such there might be a layer to my imports I am not factoring in.

When I cd into app/ and run set FLAKS_ENV=development and flask run I receive the following ModuleNotFoundError:

Traceback (most recent call last):
  File "c:\users\den\code\flaskapps\contactform\venv\lib\site-packages\flask\cli.py", line 256, in locate_app
    __import__(module_name)
  File "C:\Users\den\code\FlaskApps\ContactForm\app\app.py", line 3, in <module>
    from extensions import db
ModuleNotFoundError: No module named 'extensions'
GrayLiterature
  • 381
  • 2
  • 13

0 Answers0