Python is a fun language. It is easy to read, write, and they have libraries for almost everything. To try and catch up though all at once I am going to go through the process of setting up the new hot stack. I will be going through this setup on my system using Mac OSX 10.8.2, and we’ll use pip and brew for installing our dependancies for this article (What the heck is pip or brew?) as it makes getting setup very easy.
Where we’ll end up:
- Python 2.7
- Separate Python environments using virtualenv to help with development down the road
- Flask micro-framework for the web server, template engine and API development
- MongoDB for our document store using the PyMongo library
- Jinja2 for the front-end template engine, bundled with Flask
- Flask-OpenID for user login
Easiest step, Mac OSX 10.8.2 is already running Python 2.7.2! If you don’t think you have it open up a terminal and type the command python –version. If you don’t it, go download and install it: http://www.python.org/getit/
$ python --version Python 2.7.2
Open up your terminal and fire off a this command to install virtualenv, you may have to use sudo to elevate the command.
$ sudo pip install virtualenv
After it is installed make a directory to store and locate any virtual environments you have setup for Python and go into that directory.
$ mkdir virtualenvs $ cd virtualenvs
To make a new virtual environment is really easy, just type the command virtualenv environmentName and it creates the directory structure for you.
$ virtualenv envTest New python executable in envTest/bin/python Installing setuptools............done. Installing pip...............done.
Go into that new directory that virtualenv created and you’ll see a full folder structure with Python 2.7 in there for you. This is important, to activate the environment on this terminal only run the command source bin/activate to get into the new virtual environment. Use the command deactiavte to get back out of the virtual environment. Any pip commands we run in here, while active, will install to this virtual environment only instead of the computers global store.
$ source bin/activate (envTest) $ pip install blah (envTest) $ deactivate $
Why mess with virtualenv at all?
When you start having multiple projects, on multiple servers, you want your local development environment to match that of the server, including things like different framework versions. Setting up and using virtual environments like this allows you to get in the good habit of knowing what is on your box and available to you, and why you chose what you did.
Installing Flask, PyMongo, and Flask-OpenID
Get back into your virtual environment directory you setup earlier and then lets run the easy pip install commands!
$ source bin/activate (envTest) $ pip install Flask (envTest) $ pip install pymongo (envTest) $ pip install Flask-OpenID (envTest) $ deactivate
I just followed the MongoDB installation instructions and it worked perfectly. Simply type the command brew install mongodb and you’re good to go.
$ brew install mongodb
Test the environment setup
So now that we have everything installed lets run a test Python script to verify that everything is working as we expected. Open up a new terminal and fire up MongoDB using the command mongod and then run this script after activating your virtual environment on the terminal you are in.
from flask import Flask from pymongo import MongoClient import flask_openid mc = MongoClient() print "PyMongo test: " print mc.database_names() print "flask_openid test: " print flask_openid.COMMON_PROVIDERS["google"] app = Flask(__name__) @app.route('/') def hello_world(): return "Flask is running!" if __name__ == '__main__': app.run()
You should see a result something like this after running it, which means that each library we installed is working correctly.
(envTest) $ python envTest.py PyMongo test: [u'local'] flask_openid test: https://www.google.com/accounts/o8/id * Running on http://127.0.0.1:5000/
Now we’re all setup and ready to code, which is what I think I’ll save for the next post.