After setting up a simple Flask app from this tutorial, with a run.py file containing:
from site import app
import os
app.secret_key = os.urandom(24)
app.run(debug=True)I have site/__init__.py with
from .views import app
from .models import graphAnd under site/views.py
from .models import User, get_todays_recent_posts
from flask import Flask, request, session, redirect, url_for, render_template, flash
app = Flask(__name__)with a bunch of @app.route functions
site/models.py has
from py2neo import Graph, Node, Relationship
from passlib.hash import bcrypt
from datetime import datetime
import os
import uuid
graph = Graph()I keep getting a ImportError: cannot import name app error on the first line of run.py.
All the other questions seem to be a circular import issue, but I'm sure I don't have one.
I haven't created a virtual environment as described in the project, could that be the issue?
I'm using python2.7 on windows.
EDIT:
A fix to the problem was to restructure entire project into one .py file, which feels like a cleaner file structure anyway.
13 Answers
Look at the source code
You'll see in site/__init__.py
from .views import app This declares app in the site module, therefore allowing you to use this at the run module
from site import appOtherwise, you need
from site.views import app 4 app is defined within site.views, so you need to import it from there.
from site.views import app 2 If people are still having this issue, changing the folder name from site to something else (e.g. website) and when you import from site change it to the new folder name works.
I suppose there is something in Flask that prevents you from naming your directory site.