1
votes

I have a very small Flask app that is laid out as follows:

tinker/
      main.py
      /my_package
              init.py
              views.py

When I do tinker>python main.py everything runs fine. Here are the contents of each file:

main.py:

from my_package import app
app.run()

my_package/init.py:

from flask import Flask

app = Flask(__name__)
from my_package import views

my_package/views.py:

from my_package import app

@app.route('/')
def home():
    return 'Ola!!!!!'

While all the above code runs fine when I try to modify it slightly by using a a create_app() code pattern, as in the below, views.py throws the following exception: "ImportError: cannot import name 'app' from 'my_package' " Is there a way to fix the problem without using Blueprints?

main.py:

from my_package import create_app
app = create_app()
app.run()

my_package/init.py:

from flask import Flask

def create_app():
    app = Flask(__name__)
    from my_package import views
    return app

my_package/views.py:

from my_package import app

@app.route('/')
def home():
    return 'Ola!!!!!'
1
In case you want to run the code that works, you can access it from a repo I have at: github.com/lfernandez55/… - Luke

1 Answers

-1
votes

init.py needs to be renamed to __init__.py

Move app = Flask(__name__) outside of the create_app method

Change to from . import app in views.py