GUI Free Life View on GitHub

Openshift V2 and Flask with Virtualenv on OS X

python openshift mac

Nov 22, 2014

Create a flask app for Openshift with a matching local python virtualenv to perform local testing.

In this case we’ll use Python 2.7 on Mac OS X 10.9.

Overview of the steps

  1. Install Homebrew
  2. Install Python Development Environment on Mac OS X
  3. Install rhc client tools.
  4. Install and Configure a Python Flask for OpenShift

Installing Homebrew

Ready the system for Homebrew

# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH

# Load brew bash completeion if it exists
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion

# pip should only run if there is a virtualenv currently activated
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
. ~/.bash_profile
xcode-select --install

This will prompt you with a GUI dialog asking you to install the command line developer tools. Click the Install button.

Install Homebrew

ruby -e "$(curl -fsSL"
# This step will possibly point out permissions problems to be fixed.
brew doctor
brew update
brew help

Install Python

Install the latest 2.7.x Python with Homebrew.

brew install python --with-brewed-openssl

Setup Virtenv

Use pip to install virtualenv.

# override the requirement we set in .bash_profile above. just this once.
PIP_REQUIRE_VIRTUALENV=false pip install virtualenv
mkdir -p ~/src ~/Projects ~/Virtualenvs

Setup Openshift

Interaction with Openshift is via the website and via the rhc client tool.

sudo gem install rhc
rhc setup

Create an Openshift App and Configure for Virtualenv

It is helpful to have a virtualenv on your development machine(s) which matches the environment of your Openshift app. This enables much quicker turnaround time for quick tests of changes, and doesn’t require a git commit and push to see the effect of a change.

cd ~/src
rhc app create flaskapp python-2.7
rhc git-clone flaskapp
echo 'venv/' >> ~/src/flaskapp/.gitignore
echo '*.egg-info/' >> ~/src/flaskapp/.gitignore
echo 'dist/' >> ~/src/flaskapp/.gitignore
git commit -am .gitignore 'ignore artifacts'
cd ~/src/flaskapp/
# create venv/ dir
virtualenv --python=python2.7 venv
# activate this virtual env
. venv/bin/activate
pip install flask flask-wtf flask-babel markdown flup 
# later we can just just use
python install
cd ~/src/flaskapp

Create Hello World Flask App

Create the required directories

cd ~/src/flaskapp/wsgi
mkdir -p app/{static,templates}
mkdir tmp
cd ~/src/flaskapp/wsgi/app

Create applications files. Pay attention Openshift has some particular requirements.

from flask import Flask  
app = Flask(__name__)  
from app import views
from app import app
def index():
    return "Hello, World!"
import os
import sys
sys.path.insert(0, os.path.dirname(__file__) or '.')
PY_DIR = os.path.join(os.environ['OPENSHIFT_HOMEDIR'], "python")
virtenv = PY_DIR + '/virtenv/'
PY_CACHE = os.path.join(virtenv, 'lib', os.environ['OPENSHIFT_PYTHON_VERSION'], 'site-packages')
os.environ['PYTHON_EGG_CACHE'] = os.path.join(PY_CACHE)
virtualenv = os.path.join(virtenv, 'bin/')

    exec(open(virtualenv).read(), dict(__file__=virtualenv))
except IOError:

from run import app as application
from app import app
if __name__ == "__main__": = True) #We will set debug false in production 

Test Flaskapp on localhost

Activate venv and run the app.

cd ~/src/flaskapp/
. venv/bin/activate
python install
curl http://localhost:5000/index

Deploy Flaskapp to Openshift

After making local changes, commit them to git, and push them to the origin. Openshift will then automagically install the required flask modules and spin up your Flaskapp.

cd ~/src/flaskapp
git add .
git commit -a -m 'Firstsies'
git push

Now go check out your new app on Openshift