How to fix “foreman’s missing output issue” on Python projects

Since we started using Heroku for most of our projects, foreman has been part of our toolkit. We figured out that if we were going to be using it on the server we might as well use it on our development environments. The only problem that we found was that we were not getting all the output from our apps on the terminal. While hacking on a piece of code I’m used to inserting simple ‘print’ statements until I get what I need. With foreman this was not working.

The solution is very simple. Just create a .env file next to your Procfile, and add this environment variable to the first line PYTHONUNBUFFERED=true That’s it, the next time you run your project with foreman you will see all the output.

You can use that .env file to put all the environment variables you need while running your project. Foreman will take care of setting them up for you. Also know that there’s a plugin for the Heroku CLI that makes easy to grab your applications config vars, and place them in your local .env, and vice versa.

My “other” solution. AppleScript?
Before I found the PYTHONUNBUFFERED=true solution, I was using an AppleScript to start my web projects. I must admit that I still use this for some projects, because it’s very fast.

The script does one simple thing very fast and very well. It opens iTerm2 and creates splits for everything I need to run a project. For the most complex project I have it run the Django development server, Redis, Celery and Brunch. I also create an extra panel ready for doing git commits on the project. The best part is that I can run the command using Alfred (with the run command) which makes it event better. Here’s a video of the script doing it’s thing on a simpler project.

Sometimes I wondered why no one has written a Procfile parser in AppleScript. Here’s the AppleScript I stitched together from various places online.

launch "iTerm"
tell application "iTerm"
activate
set myterm to (make new terminal)
tell myterm
launch session "DevelopmentEnvironment"
# Start Django Server
tell the last session to write text "workon lift && ./manage.py runserver_plus --settings=lift.settings.development"
tell the last session to set name to "Django Server"
# Start Brunch
tell i term application "System Events" to keystroke "d" using {command down}
tell the last session to write text "workon lift && fab brunchwatch"
tell the last session to set name to "Brunch"
# Panel for git
tell i term application "System Events" to keystroke "d" using {command down, shift down}
tell the last session to write text "workon lift"
tell the last session to set name to "git"
end tell
end tell