Barrett Sonntag » Pythonhttp://www.sosuke.com
Programming, Hobbies and OpinionTue, 23 Jun 2015 00:16:17 +0000en-UShourly1Getting started with Python web development: Setting up the environmenthttp://www.sosuke.com/getting-started-with-python-web-development-setting-up-the-environment/
http://www.sosuke.com/getting-started-with-python-web-development-setting-up-the-environment/#commentsSat, 02 Feb 2013 04:06:56 +0000http://www.sosuke.com/?p=227Continue reading →]]>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.

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

Installing virtualenv

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.

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/activateto 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.

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!

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.