This guide assumes you already have your application built and working with the correct dependencies. If you don't have pip installed or are not running from a virtual environment, you need both of those. Heroku uses pip to manage dependencies and virtualenv to sandbox your application.
Pip can be installed from the instructions here. If virtualenv is not installed, run
$ pip install virtualenv
from the command line. Then, navigate to the root directory of your app
$ cd ~/path/to/your/app
$ virtualenv venv --distribute
$ . venv/bin/activate
This will start a virtual environment for your app and activate it so that any python execution will run from that environment. While the environment is active, install all of the dependencies with pip
$ pip install <module name and version>
If you already have a virtualenv for your app, activate it. Run the command
$ pip freeze > requirements.txt
which will output the names and versions of all dependencies for your app in a version usable by Heroku. Heroku will use this to ensure that the correct modules are installed in your virtualenv.
You'll also need a .gitignore file in the directory, so create this and put
venv
*.pyc
in it.
Now create a file called Procfile and put
web: python <file that starts the app>.py $PATH
in the file. This tells Heroku how to start your app. This is the general means of setting up an app to run on Heroku, but web.py has some extra requirements. Heroku assigns a port number for your app to run on, but web.py requires the port number to be passed in as a command-line argument when the app is started. There is no built-in means of changing the port number from within web.py, so we have a conundrum. One way around this is to write another script to start the server after determining the port number. This looks like
import os
import subprocess
import sys
path = str(os.environ.get('PORT', 8080))
subprocess.call(["python", sys.argv[1], path])
Rewrite the Procfile to run this fileweb: python <file that the above code is in>.py <file that starts the app>.py $PATH
Now, place this on your Heroku server with
$ heroku login
$ git init
$ git add .
$ git commit -m "Initial commit"
$ heroku create
$ git push heroku origin
$ heroku ps:scale web=1
$ heroku open
If the heroku command does not work, install the Heroku Toolbelt.
And your app should be running. The last command should open it in a browser, which will let you know if there are any problems with the app.
No comments:
Post a Comment