14

I cannot run gunicorn on heroku with simple flask app.
The application is really simple. This is app.py:

app = Flask(__name__)

@app.route("/")
def say_hello(url):
    return "Hello"


if __name__ == "__main__":
    port = int(os.environ.get('PORT', 8888))
    app.run(host='0.0.0.0',port=port)

The app works fine through flask test server on heroku, but when I switch to use gunicorn, it crashes with:

ImportError: No module named app.wsgiapp

My requirements.txt:

Flask==0.8
gevent==0.13.7
gunicorn==0.13.2

I've tried different versions of gunicorn from 0.13.7 to 0.14.6 with no success.

Procfile:

web: gunicorn app:app -w 4 -b 0.0.0.0:$PORT

Running this command:

heroku logs

gives this:

←[33m2012-08-09T21:08:02+00:00 app[web.1]:←[0m ImportError: No module named app.
wsgiapp ←[33m2012-08-09T21:08:02+00:00 app[web.1]:←[0m     entry = __import__(self.modul
e_name, globals(),globals(), ['__name__'])

Any help please?

Patrick Yoder
  • 1,065
  • 4
  • 14
  • 19
Joseph
  • 3,085
  • 3
  • 23
  • 33
  • Have you tried a newer gunicorn version, e.g. 0.14.6 like heroku specify in their example? https://devcenter.heroku.com/articles/python/#using-a-different-wsgi-server – orip Aug 09 '12 at 21:46
  • yes, does not change anything – Joseph Aug 09 '12 at 22:16
  • 4
    That can't be the whole app.py file. Add in the imports from the top. – Graham Dumpleton Aug 09 '12 at 23:25
  • for sure it is there, it is stripped for brevity, i mentioned that it works on heroku without gunicorn – Joseph Aug 10 '12 at 00:02
  • @Joseph what do you mean "stripped for brevity"? The ImportError is probably happening because of a module you're importing that's missing some requirements. Any import statements you have in your script are probably the key to figuring out the issue. – culix Aug 17 '12 at 01:15
  • @Joseph Are you able to run gunicorn locally? If so do you get the same error? – culix Aug 17 '12 at 01:16
  • it run locally perfectly – Joseph Aug 17 '12 at 04:38
  • 2
    @Joseph what did you do to fix this? I'm having the same issue right now. I have no idea why it went from "working" to "not work" with gunicorn. – wheaties Nov 29 '12 at 03:18
  • 1
    Same here, simplest possible config and it was working before I switched over to using a gunicorn config file – Peter M. Elias Dec 16 '12 at 20:39

4 Answers4

11

In my case, I got this error by having a gunicorn.py file in my top level folder. This clashed with the installed gunicorn library on Heroku.

So my run command that caused the problem was:

gunicorn -c gunicorn.py myapp:main

Raising the following error:

Traceback (most recent call last):
  File "/app/.heroku/python/bin/gunicorn", line 9, in <module>
    load_entry_point('gunicorn==18.0', 'console_scripts', 'gunicorn')()
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/app/.heroku/python/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named app.wsgiapp

Whereas after a mv gunicorn.py gunicorn_config.py it worked fine with:

gunicorn -c gunicorn_config.py myapp:main
user835325
  • 131
  • 1
  • 3
4

I ran into this problem when upgrading Ubuntu to 14.04 LTS.

For some reason, gunicorn failed to pick up the correct python path to resolve the wsgi module.

I resolved this, for now, by declaring the python path explicitly to gunicorn via the --pythonpath parameter (documented here).

For example:

gunicorn --pythonpath /path/to/containing/directory "app.wsgi_app:wsgi_app"
tohster
  • 6,973
  • 5
  • 38
  • 55
3

I finally figured this one out.

It's basically just a PATH problem. If you import certain modules (like os and sys) in the wrong order depending on your setup, you will cause Gunicorn to look in the wrong package for the app.wsgiapp module. (not to be confused with the app.wsgi_app function in Flask)

The correct import order will vary depending on your setup, but the rule of thumb based on what I was able to get working was to just make sure that your sys module is imported before your os module.

Beyond that, assuming the rest of the config is normal (as above) you shouldn't have any issues.

Note: THIS IS ONLY A PROBLEM ON HEROKU with Gunicorn. It has something to do with how their PYTHONPATH and module search path is set up. I don't know why exactly, but this is only necessary for the production environment, local setups will work fine regardless of the module import order.

Peter M. Elias
  • 1,204
  • 10
  • 22
1

My guess is there is an other "app" module in the python path (both gunicorn and flask have a module called app already). Rename it anything else than app.py and it should work.

gwik
  • 679
  • 5
  • 9
  • I do not think so, because when i rename the module to something like another_app i got the same error with the new module name – Joseph Aug 18 '12 at 05:39
  • 2
    Can you log repr(app) an PYTHON_PATH env var before it crashes ? – gwik Aug 18 '12 at 16:18
  • How? Gunicorn itself is crashing.... it does not even go to my code... any snippit of code to demonestrate how can i do that? – Joseph Aug 18 '12 at 20:13
  • 1
    Well it tries to access wsiapp which is an existing property of the flask app. So your code must be loaded. Just put something in the body of your module file. Also run gunicorn with --log-level=DEBUG – gwik Aug 19 '12 at 01:05
  • This was what I found to be the case, see my answer regarding changing import orders above. – Peter M. Elias May 12 '15 at 21:38