How to debug python or Django in Heroku

I recently had a problem where I was able to run a python script on my local virtual environment, but not on a heroku server. Doh! How do I debug this?!

(We’re running django inside of virtualenv ala heroku’s recommendations, which are pretty standard.)

Now, this is a web-app with a heavy js front-end, so debugging to the browser is not easy/convenient enough to be a practical solution.  In my local environment, I usually fix this by using “print” statements all over my code, that I read in a terminal running the “python runserver” process.

Problem, you can’t observe this directly for code running on heroku.

Solution: write the print statements into your code and commit them w/ git. (Do this on a separate branch so you don’t pollute your master, or even development branch, with debug garbage).  Deploy that stuff to a heroku server (we use a separate staging server for testing purposes).  Run the script (all our stuff launches from a browser).  Then run the following to see the output:

$ heroku logs -t --app [your-heroku-app-name]

This definitely falls into the category of a dirty-hack, but hey, it gets the job done!

One Reply to “How to debug python or Django in Heroku”

Leave a Reply

Your email address will not be published. Required fields are marked *