Running Django management commands is easy on Heroku. For example, to syncdb you simply execute:
$ heroku run python your_app/manage.py syncdb
Easy enough. But you may find that running a custom management command to be a little trickier. You might run into something like this:
$ heroku run python your_app/manage.py your_custom_command
Running python your_app/manage.py your_custom_command attached to terminal... up, run.2
Unknown command: 'your_custom_command'
Type 'manage.py help' for usage.
Ouch! And you wouldn't be alone here:
- Django custom commands not showing up on Heroku
- Running Django custom manage.py task on Heroku - Importing Issues
- Custom management command discovery fail :(
The solution
This really just comes down to Python not finding your app. You just need to adjust your Python path to include your home directory.
On Heroku, your home directory is generally "/app". You should confirm this by running:
$ heroku run env | grep HOME
HOME=/app
A simple way to adjust your Python path within your Heroku environment (and not mucking with your app) is by setting the PYTHONPATH env variable as follows:
$ heroku config:add PYTHONPATH=/app
To confirm it is set correctly, run:
$ heroku run env | grep PYTHONPATH
PYTHONPATH=/app
Now you can run your custom management command. This also allows you to run these as cron (scheduled) tasks:
$ heroku run python your_app/manage.py your_custom_command
Success!
I hope this post will save you some headbanging. Unless, of course, it's to the Melvins.