<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sosuke &#187; Python</title>
	<atom:link href="http://www.sosuke.com/index.php/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sosuke.com</link>
	<description>Dark music of the gods.</description>
	<lastBuildDate>Sat, 02 Feb 2013 04:06:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Getting started with Python web development: Setting up the environment</title>
		<link>http://www.sosuke.com/index.php/2013/02/01/getting-started-with-python-web-development-setting-up-the-environment/</link>
		<comments>http://www.sosuke.com/index.php/2013/02/01/getting-started-with-python-web-development-setting-up-the-environment/#comments</comments>
		<pubDate>Sat, 02 Feb 2013 04:06:56 +0000</pubDate>
		<dc:creator>Barrett Sonntag</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.sosuke.com/?p=227</guid>
		<description><![CDATA[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 [...]]]></description>
				<content:encoded><![CDATA[<p>Python is a <strong>fun</strong> language. It is easy to read, write, and they have libraries for <a href="http://xkcd.com/353/">almost everything</a>. 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&#8217;ll use pip and brew for installing our dependancies for this article (What the heck is <a href="http://www.pip-installer.org/">pip</a> or <a href="http://mxcl.github.com/homebrew/">brew</a>?) as it makes getting setup very easy.</p>
<p>Where we&#8217;ll end up:</p>
<ul>
<li><a href="http://www.python.org/">Python</a> 2.7</li>
<li><span style="line-height: 13px;">Separate Python environments using <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> to help with development down the road</span></li>
<li><a href="http://flask.pocoo.org/">Flask</a> micro-framework for the web server, template engine and API development</li>
<li><a href="http://www.mongodb.org/">MongoDB</a> for our document store using the <a href="http://api.mongodb.org/python/current/">PyMongo</a> library</li>
<li><a href="http://jinja.pocoo.org/docs/">Jinja2</a> for the front-end template engine, bundled with Flask</li>
<li><a href="http://packages.python.org/Flask-OpenID/">Flask-OpenID</a> for user login</li>
</ul>
<p><strong>Installing Python</strong></p>
<p>Easiest step, Mac OSX 10.8.2 is already running Python 2.7.2! If you don&#8217;t think you have it open up a terminal and type the command <em>python &#8211;version</em>. If you don&#8217;t it, go download and install it: <a href="http://www.python.org/getit/">http://www.python.org/getit/</a></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ python --version
Python 2.7.2</pre></td></tr></table></div>

<p><strong>Installing virtualenv</strong></p>
<p>Open up your terminal and fire off a this command to install virtualenv, you may have to use <a href="http://www.sudo.ws/">sudo</a> to elevate the command.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ sudo pip install virtualenv</pre></td></tr></table></div>

<p>After it is installed make a directory to store and locate any virtual environments you have setup for Python and go into that directory.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ mkdir virtualenvs
$ cd virtualenvs</pre></td></tr></table></div>

<p>To make a new virtual environment is really easy, just type the command <em>virtualenv environmentName</em> and it creates the directory structure for you.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ virtualenv envTest
New python executable in envTest/bin/python
Installing setuptools............done.
Installing pip...............done.</pre></td></tr></table></div>

<p>Go into that new directory that virtualenv created and you&#8217;ll see a full folder structure with Python 2.7 in there for you. This is important, to activate the environment <strong>on this terminal only </strong>run the command <em>source bin/activate</em><em> </em>to get into the new virtual environment. Use the command <em>deactiavte</em> to get back out of the virtual environment. Any <strong>pip</strong> commands we run in here, while active, will install to this virtual environment only instead of the computers global store.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ source bin/activate
(envTest) $ pip install blah
(envTest) $ deactivate
$</pre></td></tr></table></div>

<p><strong>Why mess with virtualenv at all?</strong></p>
<p>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.</p>
<p><strong>Installing Flask, PyMongo, and Flask-OpenID</strong></p>
<p>Get back into your virtual environment directory you setup earlier and then lets run the easy pip install commands!<em><br />
</em></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ source bin/activate
(envTest) $ pip install Flask
(envTest) $ pip install pymongo
(envTest) $ pip install Flask-OpenID
(envTest) $ deactivate</pre></td></tr></table></div>

<p><strong>Install MongoDB</strong></p>
<p>I just followed the <a href="http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/">MongoDB installation</a> instructions and it worked perfectly. Simply type the command <em>brew install mongodb </em>and you&#8217;re good to go.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">$ brew install mongodb</pre></td></tr></table></div>

<p><strong>Test the environment setup</strong></p>
<p>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 <em>mongod </em> and then run this script after activating your virtual environment on the terminal you are in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> flask <span style="color: #ff7700;font-weight:bold;">import</span> Flask
<span style="color: #ff7700;font-weight:bold;">from</span> pymongo <span style="color: #ff7700;font-weight:bold;">import</span> MongoClient
<span style="color: #ff7700;font-weight:bold;">import</span> flask_openid
&nbsp;
mc <span style="color: #66cc66;">=</span> MongoClient<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;PyMongo test: &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> mc.<span style="color: black;">database_names</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;flask_openid test: &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> flask_openid.<span style="color: black;">COMMON_PROVIDERS</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;google&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
app <span style="color: #66cc66;">=</span> Flask<span style="color: black;">&#40;</span>__name__<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">@</span>app.<span style="color: black;">route</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> hello_world<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Flask is running!&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'__main__'</span>:
	app.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You should see a result something like this after running it, which means that each library we installed is working correctly.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">(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/</pre></td></tr></table></div>

<p>Now we&#8217;re all setup and ready to code, which is what I think I&#8217;ll save for the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sosuke.com/index.php/2013/02/01/getting-started-with-python-web-development-setting-up-the-environment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
