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

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

  1. Does this still work? I can see print and access statements on heroku with heroku logs, but locally I don’t see anything but startup output for foreman or honcho. PYTHONUNBUFFERED=true in my .env file, which is being loaded successfully, as other settings in it are respected.

    1. It actually does still work! I just had to run foreman like this:

      foreman start -e .env

      with the above in my file. Does this work for you?

Leave a Reply