tchaymore
tchaymore

Reputation: 3726

Configuring gunicorn for Django on Heroku

I'm trying to setup a test Django project on Heroku. Following the advice here and in the Heroku Getting Started I'm trying to use gunicorn instead of the Django dev server.

This was my first attempt at the Procfile:

web: gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_project/settings.py
worker: python my_project/manage.py celeryd -E -B --loglevel=INFO

This gave me this error:

ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named py

I decided to take a different track and followed the advice here. Now my Procfile looked like this:

web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload my_project.settings

(I also updated my requirements file to include gevent.) It gave me the same error:

ImportError: Could not import settings

Finally, I just set it to settings:

web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload settings

But now I get this error:

Error: django project not found

The way my Django project is set up is that the settings.py file is in the parent directory of the repo -- I don't have the Django project under another directory. It's at the same level as the virtualenv and git files. Would that be a problem? I'm sure I'm doing something simple wrong -- any help would be much appreciated.


If I follow the instructions from Heroku here and change the Procfile to this:

web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT

Nothing happens -- no errors in the logs, but no proceses run and the app just appears dead in the water.

Upvotes: 6

Views: 15236

Answers (4)

darren
darren

Reputation: 19399

I had this issue and landed up having to point directly to the python path and then set the settings reference.

In the end my Procfile looks like this:

web: gunicorn_django --pythonpath=/app/project --settings=settings

I had to run heroku run which showed the env variables and that's where I was able to find the /app which I prepended to my project name.

Upvotes: 2

Kin
Kin

Reputation: 1397

I had the same exact issue that you are having. The way I was able to finally get it working was to use the django app gunicorn.

I added gunicorn to the django settings.py

'gunicorn',

I then used this as my web entry in my Procfile.

web: python manage.py run_gunicorn -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload

You may have to alter you .manage.py if you use a different directory structure then I did. My app was in /app, and my python path was also /app.

Upvotes: 4

J C
J C

Reputation: 260

I have just run into this same issue. In the procfile you copied from the Heroku guide, change hellodjango.wsgi to yourproject.wsgi

Looks like we all fall victim to blindly copy-pasting now and then, but in your (and my) defense, it looks like there's no *.wsgi file that's actually being opened, it's just how you signal to gunicorn that you want it to run your django project.

Upvotes: 15

Jon Mountjoy
Jon Mountjoy

Reputation: 4526

Do you have a requirements.txt in the root folder (containing the word django), as well as a settings.py? Those appear to the be the requirements for Django app detection, as documented here.

Upvotes: 0

Related Questions