title: Confusion regarding Flask & importing modules
body: <p>I have a flask app that i'm trying to organize and follow the proper flask folder structure and module/importing that's been shown in many tutorials. </p>
  
  <p>I'm at the point where I actually do not know <strong>why</strong> i'm doing certain things, and that's never a good thing. </p>
  
  <p>My flask app is laid out as follows:</p>
  
  <pre><code>/steam
      run.py
      /steamapp
          __init__.py
          config.py
          tasks.py
          views.py
          /static
             css.css
          /templates
             template.html
  </code></pre>
  
  <h1>run.py</h1>
  
  <pre><code>from steamapp import app
  app.run()
  </code></pre>
  
  <h1><strong>init</strong>.py</h1>
  
  <pre><code>from celery import Celery
  from config import secrets, constants
  from flask.ext.openid import OpenID
  from flask import Flask
  import praw
  
  def make_celery(app):
      celery = Celery(app.import_name, backend='amqp', broker='amqp://guest@localhost//')
      celery.conf.update(app.config)
      TaskBase = celery.Task
      class ContextTask(TaskBase):
          abstract = True
          def __call__(self, *args, **kwargs):
              with app.app_context():
                  return TaskBase.__call__(self, *args, **kwargs)
      celery.Task = ContextTask
      return celery
  
  
  app = Flask(__name__)
  oid = OpenID(app)
  app.debug = True
  app.secret_key = secrets.APP_SECRET_KEY
  celery = make_celery(app)
  
  
  reddit = praw.Reddit(constants.USERAGENT)
  reddit.login(constants.USERNAME, secrets.PASSWORD)
  
  rs = praw.Reddit(constants.USERAGENT)
  rs.set_oauth_app_info(secrets.CLIENT_ID, secrets.CLIENT_SECRET, constants.REDIRECT_URL)
  
  
  from steamapp import views
  from steamapp import tasks
  </code></pre>
  
  <h1>views.py</h1>
  
  <pre><code>from steamapp import app, oid, celery
  from tasks import get_auth_url, check_reddit_oauth, request_steam_api
  from flask import Flask, session, redirect, request, render_template, url_for
  from config import secrets, constants
  from flask.ext.openid import OpenID
  import os
  import praw
  import time
  import json
  import string
  import random
  import requests
  
  
  def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
      return ''.join(random.choice(chars) for _ in range(size))
  
  
  def check_session_id(session):
      if "id" in session:
          return True
      else:
          return render_template("error.html", error=["?", "No session ID found."])
  
  
  @app.route("/register")
  def register():
      if "id" not in session:
          session["id"] = id_generator()
      authorize_url = get_auth_url(session)
      return redirect(authorize_url)
  
  
  @app.route("/redirect/")
  @oid.loginhandler
  def redirect_to_steam_oauth():
     ...
     ...
     ...
     etc
  </code></pre>
  
  <h1>tasks.py</h1>
  
  <pre><code>from steamapp import celery, rs
  from config import secrets, constants
  import requests
  
  @celery.task
  def get_auth_url(session):
      return rs.get_authorize_url(session["id"], scope="identity")
  </code></pre>
  
  <p>When I run run.py I get the following traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/andy/Desktop/Python Projects/Finished/steam/run.py", line 1, in &lt;module&gt;
      from steamapp import app
    File "/home/andy/Desktop/Python Projects/Finished/steam/steamapp/__init__.py", line 25, in &lt;module&gt;
      from steamapp import views
    File "/home/andy/Desktop/Python Projects/Finished/steam/steamapp/views.py", line 20, in &lt;module&gt;
      @app.route("/register")
  NameError: name 'app' is not defined
  </code></pre>
  
  <p>I am having trouble understanding what is going on as a whole - I need an explanation for why i'm doing the things i'm doing:</p>
  
  <ol>
  <li><p>What is reason for seperating out <code>app.run()</code> into a seperate python file that imports app from steamapp? Why is the app.run() not in views.py?</p></li>
  <li><p>What is <strong>init</strong>.py actually meant to do? Why are the <code>views.py</code> and <code>tasks.py</code> imported into it? </p></li>
  <li><p>Why is it giving the traceback of "app" not being defined when it's being imported from <code>__init__.py</code>?</p></li>
  </ol>
  
  <p>Thank you to anyone who could provide some clarification, as I have lost the sense of what is going on.</p>
  
  <hr>
  
  <h1>EDIT:</h1>
  
  <p>I've updated the code in the OP with changes i've made after reading @dreamriver's response. </p>
  
  <p>I'm still a bit confused - why am I importing flask in views.py when it is already imported in <strong>init</strong>.py, where views is imported and presumably ran from? </p>
  
  <p>Also, how does a celery worker tie into this? Setting tasks.py as a Celery worker returns the following traceback:</p>
  
  <pre><code> celery -A tasks worker --loglevel=info
  Traceback (most recent call last):
    File "/usr/local/bin/celery", line 11, in &lt;module&gt;
      sys.exit(main())
    File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
      main()
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
      cmd.execute_from_commandline(argv)
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
      super(CeleryCommand, self).execute_from_commandline(argv)))
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 305, in execute_from_commandline
      argv = self.setup_app_from_commandline(argv)
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 465, in setup_app_from_commandline
      self.app = self.find_app(app)
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 485, in find_app
      return find_app(app, symbol_by_name=self.symbol_by_name)
    File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 229, in find_app
      sym = symbol_by_name(app, imp=imp)
    File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 488, in symbol_by_name
      return symbol_by_name(name, imp=imp)
    File "/usr/local/lib/python2.7/dist-packages/kombu/utils/__init__.py", line 92, in symbol_by_name
      module = imp(module_name, package=package, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/celery/utils/imports.py", line 101, in import_from_cwd
      return imp(module, package=package)
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
      __import__(name)
    File "/home/andy/Desktop/Python Projects/Finished/steam/steamapp/tasks.py", line 1, in &lt;module&gt;
      from steamapp import celery, rs
  ImportError: No module named steamapp
  </code></pre>