Sunday, October 21, 2012

Getting started on Heroku with python for Mac

Was trying to get Heroku up and running, but ran into some trouble that's not documented because my Mac >= 10.7. This may be useful for beginners.

To make a heroku app, you need virtualenv, which you need pip to install easily. To get pip:
1. Install homebrew (the instructions provided by heroku's link to installing python for Macs (which have a weaker version of Python) is outdated)
- scroll all the way down to copy and paste the CLI text from the 'Install Homebrew' box
- you may need to run '$ brew link python' if there was a warning saying that it was not linked
-- if prompted to force link because some files already exist, do so:
$ brew link --overwrite python
- run '$ brew doctor' to check your homebrew
- if there is the warning that your PATH prefers 'usr/bin' to 'usr/local/bin', run:
$ echo 'export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"' >> ~/.bash_profile
then Quit and Open Terminal again, run '$ brew doctor', and the warning should be gone.
- The message 'Your system is raring to brew' would be the most optimal setup of homebrew, but you may have macports and stuff that gets in the way. That's OK, you can still install virtualenv
- This is a great intro to homebrew
2. Install pip
$ brew install pip

Logging into heroku on the CLI with Credentials, throws error 'Cannot allocate memory...'
- Reinstall the heroku Toolbelt. You don't have to uninstall or remove the toolbelt because heroku takes care of it for you.

Setup a virtualenv in your app's folder
- If running '$ virtualenv --distribute venv' outputs that the command virtualenv doesn't exist, it's because virtualenv is in the python package that pip installed.
$ python2.7 ~/../../usr/local/lib/python2.7/site-packages/virtualenv.py --distribute venv

Activate the virtual environment to run your app, which you need to do everytime you open your app for development.
$ source venv/bin/activate

As per the Heroku guide, make your dependencies text with '$ pip freeze > requirements.txt' and  Procfile (file name must be 'Procfile')  with 'web: python <your_file>.py' inside the text editor.

$ foreman start
- Say you have 'import jinja2' but it can't find the library. In your application, if you import libraries like jinja2 or webapp2, run '$ pip install jinja2' in the application folder.
- If using Python, and after '$ foreman start' you don't see anything after 'started with pid XXXX', create an '.env' file with 'PYTHONUNBUFFERED=TRUE' inside.
- Everytime you update <your_file>.py, Ctrl-C, '$ foreman start' to restart the process. The debug log will change on the local site, but not the actual code running underneath. It's not Google App Engine where the dev app is actually live.

No comments:

Post a Comment