djangoherokuprocfilewaitress

Waitress on Heroku giving error


I am trying to switch from Gunicorn to Waitress on Heroku. In the logs, I keep getting an error from Waitress:

Error: Bad module 'cardisle'

In my procfile, I have:

web: waitress-serve --port=$PORT cardisle.wsgi:application

If I remove the .wsgi extension, I get a different error:

Error: Bad object name 'application'

I have tried changint the object name to wsgifunc as well since it's in the Waitress doc, but no luck.

Any help would be appreciated. I have a wsgi.py file with the following:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardisle.settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Solution

  • Here's an awful fact about waitress: it is hiding info from you.

    If you look at the source, "Bad Module" is code for "There was a failure importing your application from the wsgi module."

    To see the error, try:

    1. logging into a dyno with heroku run bash
    2. navigating to the directory with wsgi.py in it (with cd)
    3. opening a shell with python
    4. running import wsgi

    When I hit this error and did this, I got:

    ~/proj/proj $ python
    
    Python 2.7.9 (default, Dec 11 2014, 17:18:51) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wsgi
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "wsgi.py", line 36, in <module>
        application = get_wsgi_application()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
        django.setup()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
        app_config = AppConfig.create(entry)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/config.py", line 86, in create
        module = import_module(entry)
      File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
    
    ImportError: No module named debug_toolbar
    

    Which is a much more helpful error. In my case, I had set DJANGO_SETTINGS_MODULE to 'local' in production, (which did not have the appropriate requirements,) and so the import failed.

    The exact nature of your problem will vary, but I'll mention a case that frustrated me when I started out:

    If you are running web: waitress-serve --port=$PORT cardisle.wsgi:application, you may need to change your PYTHONPATH environment variable so that PYTHONPATH+cardisle.wsgi is a fully-formed extant path on the machine in question.

    I'll open a PR for waitress this evening that tries to bubble up the import error. Best of luck otherwise!