title: My flask application factory returns URL not found
body: <p>My flask app is based on Miguel Grinberg's mega tutorial XIV with some extra stuff and it was all working fine and could be accessed from browser on localhost:5000. I decided to switch to an application factory approach as per Grinberg's XV tutorial BUT with no blueprints. Now when I enter localhost:5000 I get a URL not found. I am guessing my routes.py is not being picked up for some reason. </p>
  
  <p>I have thoroughly worked through Grinberg's XV tutorial and the associated code and aligned everything less blueprints. These are the links I have explored that I have based my current app on: -</p>
  
  <p><a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure" rel="nofollow noreferrer">https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure</a></p>
  
  <p><a href="http://flask.pocoo.org/docs/1.0/tutorial/factory/" rel="nofollow noreferrer">http://flask.pocoo.org/docs/1.0/tutorial/factory/</a></p>
  
  <p>Dave W Smith's answer in <a href="https://stackoverflow.com/questions/50355269/configure-python-flask-app-to-use-create-app-factory-and-use-database-in-model">Configure Python Flask App to use &quot;create_app&quot; factory and use database in model class</a></p>
  
  <p>and others.</p>
  
  <p>From what I have read implementing an application factory should be really simple. The examples work. </p>
  
  <p>The flask server starts from the command prompt in a venv as always.</p>
  
  <p>Here is my directory structure.</p>
  
  <pre><code>myapp
      app.py
      config.py
      ...
      /app
          __init__.py
          routes.py
          models.py
          forms.py
          ...
  </code></pre>
  
  <p>Here is the code slightly simplified for clarity.</p>
  
  <pre><code># app.py
  #=============================================
  from app import create_app, db
  from app.models import User, Post
  
  app = create_app()
  
  @app.shell_context_processor
  def make_shell_context():
      return {'db': db, 'User': User, 'Post' :Post}
  
  # __init__.py
  #=============================================
  ...
  import os
  from flask import Flask, request, current_app
  from flask_sqlalchemy import SQLAlchemy
  from flask_migrate import Migrate
  ...
  from config import Config
  
  db = SQLAlchemy()
  migrate = Migrate()
  ...
  
  def create_app(config_class=Config):
      app = Flask(__name__)
      app.config.from_object(config_class)
  
      db.init_app(app)
      migrate.init_app(app, db)
      ...
  
      # logging, email servers, etc configured
  
      return app
  
  from app import models
  
  # routes.py
  #=============================================
  from datetime import datetime
  from flask import render_template, flash, redirect, url_for, request, g, \
      jsonify, current_app
  ...
  from app import app, db
  from app.forms import LoginForm, RegistrationForm, EditProfileForm, 
       PostForm,ResetPassword,RequestForm, ResetPasswordForm
  from app.models import User, Post
  ...
  
  @app.route('/', methods=['GET', 'POST'])
  @app.route('/index', methods=['GET', 'POST'])
  @login_required
  def index():
      form = PostForm()
      if form.validate_on_submit():
          ...
      page = request.args.get('page', 1, type=int)
      ...
      return render_template('index.html', title=_('Home'), form=form,
                             posts=posts.items, next_url=next_url,
                             prev_url=prev_url)
  
  @app.route ....
  
  
  # models.py
  #=============================================
  from datetime import datetime
  from hashlib import md5
  from time import time
  from flask import current_app
  from flask_login import UserMixin
  from werkzeug.security import generate_password_hash, check_password_hash
  import jwt
  from app import db, login
  
  class User(UserMixin, db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(64), index=True, unique=True)
      ...
  
  </code></pre>
  
  <p>This may be irrelevant but it is one thing I tried. Note in routes.py that I have <code>from app import app, db</code>, if I remove <code>app</code> and add <code>from flask import current_app</code> then the <code>@app</code>  decorators show as undefined.</p>
  
  <p>Another point, because its a problem with routes. If I change the last line of <code>__init__.py</code> to <code>from app import routes, models</code> I get a different error.</p>
  
  <pre><code>flask.cli.NoAppException
  flask.cli.NoAppException: While importing "metapplica", an ImportError was raised:
  
  Traceback (most recent call last):
    File "c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\flask\cli.py", line 236, in locate_app
      __import__(module_name)
    File "C:\Users\Mark Kortink\Dropbox\Python\projects\metapplica\metapplica.py", line 1, in &lt;module&gt;
      from app import create_app, db, cli
    File "C:\Users\Mark Kortink\Dropbox\Python\projects\metapplica\app\__init__.py", line 75, in &lt;module&gt;
      from app import routes, models
    File "C:\Users\Mark Kortink\Dropbox\Python\projects\metapplica\app\routes.py", line 8, in &lt;module&gt;
      from app import app, db
  ImportError: cannot import name 'app' from 'app' (C:\Users\Mark Kortink\Dropbox\Python\projects\metapplica\app\__init__.py)
  </code></pre>
  
  <p>It was this error that led me to try the <code>current_app</code> change mentioned above.</p>
  
  <p>I start the app like this.</p>
  
  <pre><code>cd C:\Users\...\myapp
  venv\Scripts\activate 
  set FLASK_APP=app.py
  set FLASK_DEBUG=1
  flask run
  </code></pre>
  
  <p>I know the problem is probably really basic but can anyone see why the above code would give a URL not found or not be able to find the routes?</p>
  
  <p>=== NEXT PHASE ================
  Following the recommendations from @silver below my new names are: -</p>
  
  <pre><code>myapp
      runapp.py
      ...
      /mapp
          __init__.py
          ...
  </code></pre>
  
  <p>I went through all my code and changed <code>from app[.xxx] import yyy</code> to <code>from myapp[.xxx] import yyy</code>. This flushed out a few new errors where I was referencing <code>app</code> which I fixed by substituting <code>current_app</code>. 
  My new error is </p>
  
  <pre><code>RuntimeError
  RuntimeError: Working outside of application context.
  
  This typically means that you attempted to use functionality that needed
  to interface with the current application object in some way. To solve
  this, set up an application context with app.app_context().  See the
  documentation for more information.
  
  Traceback (most recent call last)
  File "C:\Users\Mark Kortink\Dropbox\Python\projects\myapp\runapp.py", line 1, in &lt;module&gt;
  from mapp import create_app, db, cli
  File "C:\Users\Mark Kortink\Dropbox\Python\projects\myapp\mapp\__init__.py", line 75, in &lt;module&gt;
  from mapp import routes, models
  File "C:\Users\Mark Kortink\Dropbox\Python\projects\myapp\mapp\routes.py", line 16, in &lt;module&gt;
  @current_app.before_request
  </code></pre>