Question

I'm trying to run a custom django command as a scheduled task on Heroku. I am able to execute the custom command locally via: python manage.py send_daily_email. (note: I do NOT have any problems with the custom management command itself)

However, Heroku is giving me the following exception when trying to "Run" the task through Heroku Scheduler addon:

Traceback (most recent call last):
  File "bin/send_daily_visit_email.py", line 2, in <module>
    from django.conf import settings
ImportError: No module named django.conf

I placed a python script in /bin/send_daily_email.py, and it is the following:

#! /usr/bin/python
from django.conf import settings
settings.configure()
from django.core import management

management.call_command('send_daily_email') #delegates off to custom command

Within Heroku, however, I am able to run heroku run bin/python - launch the python shell - and successfully import settings from django.conf

I am pretty sure it has something to do with my PYTHON_PATH or visibility to Django's SETTINGS_MODULE, but I'm unsure how to resolve the issue. Could someone point me in the right direction? Is there an easier way to accomplish what I'm trying to do here?

Thank you so much for your tips and advice in advance! New to Heroku! :)

EDIT:

Per Nix's comment, I made some adjustments, and did discover that specifying my exact python path, I did get past the Django setup.

I now receive:

  File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 155, in call_command
    raise CommandError("Unknown command: %r" % name)
django.core.management.base.CommandError: Unknown command: 'send_daily_email'

Although, I can see 'send_daily_email' when I run ``heroku run bin/python app/manage.py```.

I'll keep an update if I come across the answer.

Was it helpful?

Solution

You are probably using a different interpreter.

Check to make sure shell python is the same as the one you reference in your script /usr/bin/python . It could be that there is a different one in your path, which would explain why it works when you run python manage.py but not your shell scrip which you explicitly reference /usr/bin/python.


Typing which python will tell you what interpreter is being found on your path.

OTHER TIPS

In addition, this can also be resolved by adding your home directory to your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).

Add it via the heroku config command:

$ heroku config:add PYTHONPATH=/app

That should do it! For more details: http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top