Heroku – Python Getting Started for Windows Local Development

Okay so, I spent last two days to get rid of this thing. Heroku for Python has a good tutorial on their website (https://devcenter.heroku.com/articles/getting-started-with-python). BUT, I found myself troubled following their explanations. Let’s review their steps one by one.

Introduction (https://devcenter.heroku.com/articles/getting-started-with-python#introduction)

Follow the instructions and you’re good to go.

Set up (https://devcenter.heroku.com/articles/getting-started-with-python#set-up)

Same like previous section. Download the toolbelt and install it.

Prepare the app (https://devcenter.heroku.com/articles/getting-started-with-python#prepare-the-app)

Everything is fine here. Proceed to the next section.

Deploy the app (https://devcenter.heroku.com/articles/getting-started-with-python#deploy-the-app)

Still fine, just keep going! Oh, wait. If you’re pretty new to Git, you’re usually typing “git push origin master” to push things, right? That is actually “git push <remote> master”, which in this context, it means Heroku’s git.

View logs (https://devcenter.heroku.com/articles/getting-started-with-python#view-logs)

Nothing wrong here. Proceed to the next section.

Defining a Procfile (https://devcenter.heroku.com/articles/getting-started-with-python#define-a-procfile)

Well, what is actually this Procfile thing? Well, it’s actually some kind of “environment settings”. You’ll use it if you want to develop locally. The normal Procfile is the Procfile used by Heroku, while Procfile.windows is used to develop locally in Windows environment.

Scale the app (https://devcenter.heroku.com/articles/getting-started-with-python#scale-the-app)

Nothing wrong here. Proceed to the next section.

Declare app dependencies (https://devcenter.heroku.com/articles/getting-started-with-python#declare-app-dependencies)

Install venv. After that, you’ll notice a folder called “venv” in your local app folder. Activate it. Then try to run with “foreman start web -f Procfile.windows”. Now this is where all my problem starts! You will notice that you couldn’t connect to “localhost:5000”. Try to break the batch process, then you will see some error like this:

<your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:230:in `synchronize’: can’t be called from trap context (ThreadError)
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:230:in `output_with_mutex’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:236:in `system’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:310:in `terminate_gracefully’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:45:in `block in start’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:298:in `call’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:298:in `wait2′
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:298:in `watch_for_termination’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/engine.rb:52:in `start’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/lib/foreman/cli.rb:41:in `start’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start’
from <your ruby path>/lib/ruby/gems/2.2.0/gems/foreman-0.61.0/bin/foreman:7:in `<top (required)>’
from <your ruby path>/bin/foreman:23:in `load’
from <your ruby path>/bin/foreman:23:in `<main>’
Terminate batch job (Y/N)? Y

Well, you actually don’t need to worry about the text that says we need to change the requirements.txt. All we need are Postgres and foreman v.0.61.

  • Why Postgres?

Our deployed application at Heroku has a Postgres database. We need to install Postgres just to make sure we have nearly the same environment as they do. After the installation, connect to the Postgres database using PGAdmin III or similar application (just to ensure that the local database server is up).

  • Why foreman v.061?

It seems like the latest version of foreman has some issues with Windows. I don’t know if there are other working versions, but I tried this one and it worked. From this issue (https://github.com/ddollar/foreman/issues/348), I learned that I need to type:

$ gem uninstall foreman

$ gem install foreman -v 0.61

…and voila. You have just installed foreman v.061.

Well, I think by this time you should have been able to run your app in localhost. BUT, if you access localhost:5000/db, it will tell you that it has missing table/relation or something. This happens because your local Postgres doesn’t have the database that your deployed app in Heroku has. To do this, follow these steps:

  1. Type “SET PGUSER=<your Postgres user>”, then press Enter
  2. Type “SET PGPASSWORD=<your Postgres password>”, then press Enter
  3. That two commands above will give you the credentials to use your Postgres user and password rather than Windows’ User Account.
  4. Type “heroku pg:pull <heroku database> <your desired database name> –app <your app name>”
  5. If you’re wondering about your app name, just type “heroku config”

After that, change the .env file to this:

DATABASE_URL=postgres://<your postgres username>:<your postgres password>@localhost:5432/<your postgres database name>

You might also change 5432 to any port you have chosen during Postgres installation.

Try again “foreman start web -f Procfile.windows” and voila! But, if you access localhost:5000/db it will give you error. To fix this, you must install cURL (http://www.confusedbycode.com/curl/). Then, follow these steps:

  1. Type “heroku pg:backups –app <your app>”
  2. Type “heroku pg:backups public-url”
  3. Copy all those awkward link to notepad. Remove newlines. Add curl in front of id and append ‘> backup.dump’ so it will become [curl “your-awkward-link” > backup.dump]
  4. Type “pg_restore –verbose –clean –no-acl –no-owner -h localhost -d [your-desired-db-name] backup.dump”

View your localhost:5000/db. It should be updating every time you visit it. Well, that’s all folks. Enjoy your day!


Leave a Reply - No foul language and spam please :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s