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.
Follow the instructions and you’re good to go.
Same like previous section. Download the toolbelt and install it.
Everything is fine here. Proceed to the next section.
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.
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.
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:
- Type “SET PGUSER=<your Postgres user>”, then press Enter
- Type “SET PGPASSWORD=<your Postgres password>”, then press Enter
- That two commands above will give you the credentials to use your Postgres user and password rather than Windows’ User Account.
- Type “heroku pg:pull <heroku database> <your desired database name> –app <your app name>”
- 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:
- Type “heroku pg:backups –app <your app>”
- Type “heroku pg:backups public-url”
- 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]
- 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!