Openshift V2 and Flask with Virtualenv on OS X

November 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

  • First unhide ~/Library folder.

    1. Open Finder
    2. Press shift-command-H
    3. Press command-J
    4. Check Show Library Folder
  • Now Setup shell environment Some of these settings are only relevant to later steps, but go ahead and put them all in now.

  • vim ~/.bash_profile

# 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
  • Source those changes. Ignore the -bash: brew: command not found error.
. ~/.bash_profile
  • Install command line developer tools or Xcode. You’ll need to be on the actual console, so don’t do this step over SSH.
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

  • Install Homebrew
ruby -e "$(curl -fsSL"
  • Inspect and update brew install.
# This step will possibly point out permissions problems to be fixed.
brew doctor
brew update
brew help
  • Install some brew packages
brew install bash-completion ssh-copy-id wget

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.

  • Install rhc
sudo gem install rhc
  • Configure rhc.

You’ll need to have a username and login for Openshift before proceeding.

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.

  • Create an Openshift app named Flaskapp.
cd ~/src
rhc app create flaskapp python-2.7
  • Clone App locally into ~/src/flaskapp if you created the app using the web site instead of rhc.
rhc git-clone flaskapp
  • Tell git to ignore the venv/ dir and other artifacts we are about to create.
echo 'venv/' >> ~/src/flaskapp/.gitignore
echo '*.egg-info/' >> ~/src/flaskapp/.gitignore
echo 'dist/' >> ~/src/flaskapp/.gitignore
git commit -am .gitignore 'ignore artifacts'
  • Setup a venv with the required python version inside flaskapp
cd ~/src/flaskapp/
# create venv/ dir
virtualenv --python=python2.7 venv
# activate this virtual env
. venv/bin/activate
  • Install Flask in the new venv we just activated.
pip install flask flask-wtf flask-babel markdown flup 
# later we can just just use
python install
  • Tell our app’s about our python module requirements.
cd ~/src/flaskapp
  • Modify the install_requires line to look like this:

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.

  • ~/src/flaskapp/wsgi/app/
from flask import Flask  
app = Flask(__name__)  
from app import views
  • ~/src/flaskapp/wsgi/app/
from app import app
def index():
    return "Hello, World!"
  • ~/src/flaskapp/wsgi/application - This application file is required by OpenShift
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
  • ~/src/flaskapp/wsgi/ - Called by 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


comments powered by Disqus