<?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>muhuk.com &#187; pip</title>
	<atom:link href="http://www.muhuk.com/tag/pip/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.muhuk.com</link>
	<description>know thyself</description>
	<lastBuildDate>Thu, 02 Sep 2010 07:13:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Quest For Ultimate Development/Deployment Toolset: Fabric, Pip &amp; Virtualenv</title>
		<link>http://www.muhuk.com/2010/01/quest-for-ultimate-developmentdeployment-toolset-fabric-pip-virtualenv/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=quest-for-ultimate-developmentdeployment-toolset-fabric-pip-virtualenv</link>
		<comments>http://www.muhuk.com/2010/01/quest-for-ultimate-developmentdeployment-toolset-fabric-pip-virtualenv/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 14:34:06 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[fabric]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[virtualenv]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=324</guid>
		<description><![CDATA[DISCLAIMER: This post is a summary of my short experience with the tools mentioned. There is no flash news or revolutionary recipes. If you are familiar with them, you will probably find it boring. If you are not, this post might make your introduction less painful. I stumbled upon this post following my frustration with [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>DISCLAIMER</strong>: This post is a summary of my short experience with the tools mentioned. There is no flash news or revolutionary recipes. If you are familiar with them, you will probably find it boring. If you are not, this post might make your introduction less painful.</em></p>

<p>I stumbled upon this <a href="http://clemesha.org/blog/2009/jul/05/modern-python-hacker-tools-virtualenv-fabric-pip/">post</a> following my frustration with <code>setuptools</code> after <code>virtualenv</code>ising <a href="http://www.telvee.com">telvee</a> repository. I have played with <a href="http://www.buildout.org/">buildout</a> to create repeatable deployments before. Don&#8217;t get me wrong, buildout is superb. But I have a few minor issues with it:</p>

<ul>
<li>Buildout can create an interpreter but the isolation from the host system is not a good as virtualenv. (Please correct me if I&#8217;m wrong)</li>
<li>Buildout is a dependency<sup>1</sup> whereas virtualenv is a system-wide tool.</li>
<li>It is, naturally, more work to set it up than a couple of ad-hoc bash scripts.</li>
</ul>

<p>None of these issues make buildout less awesome, I am just not comfortable enough with it. I am considering other alternatives and their strengths and weaknesses.</p>

<h3>Virtualenv</h3>

<p>I am quite comfortable with <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a>. It creates an isolated Python environment for you. For instance the following command will create an environment inside <code>test</code> directory that doesn&#8217;t have access to packages installed system-wide:</p>

<pre><code>~/$ virtualenv --no-site-packages test
</code></pre>

<p>That is of course when you activate the virtual environment with:</p>

<pre><code>~/$ cd test
~/test/$ source bin/activate
(test)~/test/$
</code></pre>

<p>Note the <code>(test)</code> prefix to your prompt. Any packages you install within this environment will be installed only for itself and won&#8217;t be available system-wide. If you have omitted <code>--no-site-packages</code> argument you could have access to globally installed packages too. When you are done with this virtual environment you can issue <code>deactivate</code> command to return to your normal shell. That&#8217;s basically what virtualenv does.</p>

<p>You can script virtualenv to a certain extend:</p>

<pre><code>#!/usr/bin/env bash

virtualenv $1
cd $1
source bin/activate

echo $PATH
echo $PYTHONPATH
</code></pre>

<p>Or you can create <a href="http://pypi.python.org/pypi/virtualenv#creating-your-own-bootstrap-scripts">bootstrap scripts</a> with virtualenv. Virtualenv doesn&#8217;t provide a clean and powerful enough API here, just a callback and two methods to modify commandline arguments.</p>

<p>Virtualenv is great at what it does. But I think setuptools as a package installer (and it&#8217;s just an installer, not a manager) cripples virtualenv. Good news is newer versions have a commandline argument <code>--distribute</code> that appereantly substitutes <a href="http://packages.python.org/distribute/">distribute</a> for setuptools.</p>

<h3>Pip</h3>

<p>My experiments with <a href="http://pip.openplans.org/">pip</a> went just fine. Except I bumped into <a href="http://stackoverflow.com/questions/1382925/virtualenv-no-site-packages-and-pip-still-finding-global-packages">this problem</a>. Globally installed pip was seeing system-wide packages when used on a virtual environment created with <code>--no-site-packages</code>. Again, good news is it is fixed in the trunk.</p>

<p>I will play with pip more once figure out how to best integrate the these applications.</p>

<h3>Fabric</h3>

<p>I see <a href="http://www.fabfile.org/">fabric</a> as the glue that binds everything together. I have known about it long before, but I never had the chance to experiment. I have read most of the documentation<sup>2</sup> and played with it a little. Basically the following script is an attempt to create a virtual environment and install pip and django on it, much like the shell script above:</p>

<pre><code>from __future__ import with_statement
import os
from fabric.api import *
from fabric import context_managers


def _get_virtualenv_location():
    location = prompt('New location: ', default='../test')
    env.envdir, env.envname = os.path.split(os.path.abspath(location))
    env.envpath = os.path.join(env.envdir, env.envname)
    print('using "%(envname)s" at "%(envdir)s" as virtualenv' % env)


def _virtualenv(command):
    with context_managers.cd(env.envpath):
        result = local('. bin/activate &amp;&amp; ' + command)
    return result


def clone():
    _get_virtualenv_location()
    with context_managers.cd(env.envdir):
        local('virtualenv --no-site-packages --clear %(envname)s' % env)
    print _virtualenv('echo $PATH')
    _virtualenv('easy_install pip')
    print _virtualenv('pip install django==1.1.1')
</code></pre>

<p>It took me a while to figure out how I can issue commands within a virtual environment. Since fabric commands don&#8217;t share state sourcing <code>activate</code> has no effect on subsequent commands. <a href="http://stackoverflow.com/questions/1180411/activate-a-virtualenv-via-fabric-as-deploy-user">This SO entry</a> helped me to write <code>_virtualenv()</code> function. It is kind of ugly making all functions but fab commands private. I think if fab used &#95;&#95;all&#95;&#95; or something similar it would be more <a href="http://www.python.org/dev/peps/pep-0020/">explicit</a>. Also a contrib module for virtualenv would be nice<sup>3</sup>.</p>

<p>Fabric has cool features such as <a href="http://docs.fabfile.org/0.9.0/usage/execution.html#failure-handling">failure handling</a> and <a href="http://docs.fabfile.org/0.9.0/api/contrib/files.html">code/config editing</a>. It is a great tool to create repeatable deployments. It feels great to be coding in python (well, to some extend). Perhaps the resulting code is a little too complex for local operations. I wish I could write a fab command that handles both local and remote deployments. I think it is not unusual to deploy on the same machine in another location. But having to SSH into <code>localhost</code> is weird, don&#8217;t you think?</p>

<p>I am sure there are better ways to accomplish the goal of the script above. Maybe there is an entirely different way to integrage fabric, virtualenv and pip. So, comments and suggestions are welcome as usual.</p>

<p>Maybe I&#8217;ll revisit buildout again as well.</p>

<hr />

<p><strong>1</strong>: You need to bootstrap (install) buildout with each development/deployment site.</p>

<p><strong>2</strong>: About one third of it I think.</p>

<p><strong>3</strong>: I would happily attempt one, once I learn fabric a little better.</p>


<p>Related posts:<ol><li><a href='http://www.muhuk.com/2010/05/how-to-install-mysql-with-fabric/' rel='bookmark' title='Permanent Link: How to Install MySQL with Fabric'>How to Install MySQL with Fabric</a></li>
<li><a href='http://www.muhuk.com/2009/05/sad-state-of-web-development-industry-in-turkiye/' rel='bookmark' title='Permanent Link: Sad State of Web Development Industry in Türkiye'>Sad State of Web Development Industry in Türkiye</a></li>
<li><a href='http://www.muhuk.com/2010/01/dynamic-translation-apps-for-django/' rel='bookmark' title='Permanent Link: Dynamic Translation Apps for Django'>Dynamic Translation Apps for Django</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/01/quest-for-ultimate-developmentdeployment-toolset-fabric-pip-virtualenv/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
