title: Flask and Gunicorn on Heroku import error
body: <p>I have a small Flask app which uses MongoEngine.</p>
  
  <p>my project structure:</p>
  
  <pre><code>/myproject
    -application.py
    -config.py
    /my_app
      -models.py
      -views.py
  </code></pre>
  
  <p>my  application.py:</p>
  
  <pre><code>#!/usr/bin/env python
  from flask.ext.mongoengine import MongoEngine
  from config import app
  import os
  app.debug = True
  
  # get config settings
  if __name__ == '__main__':
      app.config.from_object('config')
  else:
      app.config.from_object('heroku_config')
  
  # wrap app in mongengine
  db = MongoEngine(app)
  
  if __name__ == '__main__':
      # Bind to PORT if defined, otherwise default to 5000.
      port = int(os.environ.get('PORT', 5000))
      app.run(host='0.0.0.0', port=port)
  </code></pre>
  
  <p>my models.py:</p>
  
  <pre><code>from application import db
  from flask import url_for
  
  # declare model classes
  ...
  </code></pre>
  
  <p>I am deploying on heroku. If my Procfile reads:</p>
  
  <pre><code> web: python application.py
  </code></pre>
  
  <p>It works fine. When I try to switch to Gunicorn:</p>
  
  <pre><code> web: gunicorn application:app  
  </code></pre>
  
  <p>When I start gunicorn it complains by way of an import error:</p>
  
  <pre><code>ImportError: cannot import name db
  </code></pre>
  
  <p>Why is this an issue now? I'm guessing it's a path problem but I can't see why so.</p>