Waitress on Heroku giving error
Asked Answered
B

5

11

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()
Bonnee answered 2/4, 2014 at 19:51 Comment(0)
C
14

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!

Cox answered 16/5, 2015 at 22:2 Comment(1)
I made a pull request to waitress to add exception information to the error output, and the maintainer likes it. We did it everyone!Cox
M
0

The wsgi.py file should be in the cardisle directory. Waitress is trying to import cardisle.wsgi.

Musgrave answered 11/4, 2014 at 0:57 Comment(0)
M
0

I think I found the answer. If your project is laid out like this (as they are by default):

cardisle/
  cardisle/
    wsgi.py
  app1/
  app2/
  app3/

Try this Procfile instead:

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

I thought of this because Dave Hall's sample project has a different layout:

projectname/
  wsgi.py
  apps/
    app1/
    app2/
    app3/

Which has the wsgi.py file at a higher level. You probably followed his tutorial, but as far as I can tell the default Django layout doesn't quite work like that.

Mckinnon answered 12/8, 2014 at 8:22 Comment(0)
G
0

Try modifying your WSGI file like this(For Django 1.7):

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I had the same error because before, my Procfile was configured as explained on the Heroku guide:

 from django.core.wsgi import get_wsgi_application
 from dj_static import Cling

 application = Cling(get_wsgi_application())

And this was causing an issue for me.

Galina answered 24/2, 2015 at 2:11 Comment(0)
S
0

Try to use:

cardisle:wsgi.application

Not carlisle"."wsgi":"application

Stiegler answered 7/10, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.