15

My Python web application is called app

# example.py
import flask

app = flask.Flask(__name__.split('.')[0])

and when I attempt to launch it on AWS-EB using

# run.py (set correctly with WSGIPath)
from example import app

if __name__ == "__main__":
    app.run()

I get

mod_wsgi (pid=22473): Target WSGI script '/opt/python/current/app/run.py' 
    does not contain WSGI application 'application'.

How to I tell AWS that my application instance is called app?

William Miller
  • 9,839
  • 3
  • 25
  • 46
orome
  • 45,163
  • 57
  • 202
  • 418
  • Yes, good reference (but some of that wasn't working for me; see my related questions). – orome Jan 20 '15 at 18:08
  • If the basic answer doesn't work for you, you should explain what you have tried and why it doesn't work in your question. This way future readers who had the same problem will understand the answer fully. – Nick Humrich Jan 20 '15 at 18:25

2 Answers2

27

mod_wsgi expects variable called application. Try to do something like this

from example import app as application

Note: don't do application.run(). It is not needed.

Dmitry Nedbaylo
  • 2,254
  • 1
  • 20
  • 20
  • Wow, so all that's needed by AWS is an instance called `application`; the entire content of `run.py` could be that single import, correct? – orome Jan 20 '15 at 15:46
  • In fact, is it correct to say that for AWS (unlike, say, Heroku) there needs to be no *run* script at all, just a file in which the an instance called `application` is defined. – orome Jan 20 '15 at 16:00
  • Usually for development purposes, module with application contains app.run() in if __name__ == '__main__' section at the bottom of file. Good practice is to create .wsgi file that imports application. – Dmitry Nedbaylo Jan 20 '15 at 16:02
  • Yes, understood (fixed above); but is it correct that on AWS running is handled for me ('run.py' is just imported) so that I just need to *define* `application` (in the file that AWS recognizes as `WSGIPath`)? (I can implement that with a single file with `__name__=='__main__'`; but I'm just wondering in the case where I have a distinct file for AWS; all that wold need is the definition, right?) – orome Jan 20 '15 at 16:08
  • 13
    [here's a good blog entry explaining how to deal with `application`](http://blog.uptill3.com/2012/08/25/python-on-elastic-beanstalk.html). – tedder42 Jan 20 '15 at 16:26
8

While the WSGIPath can be configured. Beanstalk still expects the app variable to be named as 'application'.

A simple workaround for small single file python apps can be

from flask import Flask

app = Flask(__name__)
application = app # For beanstalk

You can keep the rest of the code as is. You just need to add that single line application = app

Abhishek J
  • 2,386
  • 2
  • 21
  • 22