It’s Been Too Long

It has been some time since my last post, things were getting a little busy for a while. Lots of work to do both for my own projects as well as for clients. In October I started a full time position with a local start up, for now I won’t make mention of what I will be doing or for who I will be doing it for, but its going to be some awesome stuff.

 

My New Years Resolution this year was to do a much better job at maintaining this site, as well as keeping to a sort of floating posting schedule. I will try and maintain at least one post per week, maybe more if I find the time. I want to also start posting reviews on items that I use, enjoy, read or like in hopes that maybe someone else thinking of getting such items will see value in them.

Freeze and Unfreeze Environments

In a previous post I explained how to setup VirtualEnv and start a new project. I however never explained the one important aspect of VirtualEnv. Saving a requirements file, a list of all installed libraries, is one of the must useful aspects. Allowing yourself or other developers to easily load up the environment you used, the same libraries, same versions. All without overwriting your changing the versions your using on your own system, or other projects.

Freezing

Let’s say your sitting in front of a newly created project, you just setup the environment, installed all the foreseeable libraries, setup the structure of your project, and create the repository and committed all your changes. Your missing one thing, a requirements file, a file outlining to yourself or anyone else that might work on your project exactly what libraries you have. Creating such a file really is not that difficult, deciding where you want to put it, that can be tricky. I always place my requirements file right at the root of my repository. To create this file just run the following command, either keep it as is to drop your file in the current working directory, or add a path to it.


pip freeze > requirements

 

You now have your very own requirements file setup specifically for your project. The only task left for you at this point, is to add it to your repository and commit the changes. If down the road you need to install new libraries, running this command and overwriting your file, is perfectly fine, just be sure to commit those changes also.

 

Unfreezing

If you are about to work on an existing project, and have this project checked out and ready to go. The first thing you will want to do is setup the environment for that project, and activate it.


virtualenv --no-site-packages --distribute [project_folder]

cd [project_folder]

source bin/activate

 

Afterwards enter the project folder, assuming the original developer used similar naming conventions and named there requirements file requirements, you can install all the required libraries.


pip install -U -r requirements

 

If the original developer didn’t use any kind of environment, and they just simply listed the requirements. You are still in a good spot, you can build the environment from here using pip to install all the packages you need then saving your own requirements file.

Python and MySQL dev on OSX

One of the biggest pains I have when developing on OSX is always trying to get MySQLdb working. Its one of those things that just doesn’t seem to be easy enough. I prefer not having MySQL running right off my desktop if I can so I will describe how I get MySQLdb running without installing the whole server, on Snow Leopard, using a VirtualEnv, and running 64bit.

 

Step 1

Download the necessary packages


wget http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.13-osx10.6-x86_64.tar.gz/from/http://mysql.mirror.rafal.ca/

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

 

Step 2

Uncompress the archives


tar -zxvf mysql-5.5.13-osx10.6-x86_64.tar.gz

tar -zxvf MySQL-python-1.2.3.tar.gz

 

Step 3

The first thing thats needed to be done is located the exact path of mysql_config, which should be located directly in the bin/ folder of the extracted files from MySQL, for my files they are located in ~/temp/mysql-5.5.13-osx10.6-x86_64/bin/ with this in mind change your working path to the root of your extracted MySQL-python-1.2.3 and modify the site.cfg file so that it now contains a mysql_config


vim site.cfg

mysql_config = ~/temp/mysql-5.5.13-osx10.6-x86_64/bin/mysql_config

 

Step 4

Now its time to build the library while still in the root of your extracted MySQL-python-1.2.3 be sure to run the build


export ARCHFLAGS="-arch i386 -arch x86_64"

python setup.py build

note: I added the export to allow for 64bit compiling under the newer versions of xcode

 

Step 5

Now that the build is complete, you don’t have any use for the extracted version of MySQL server, but we still need the libraries. Enter back into the root of the mysql-5.5.13-osx10.6-x86_64 and lets put them on our systems. Afterwards you can delete the files associated with MySQL Server (mysql-5.5.13-osx10.6-x86_64)


cd ./lib/

cp ./*.dylib /usr/lib/

 

Step 6

From here its up to you, you can install the libraries system wide or you can install them on a per environment bases


source ./bin/activate

cd ~/temp/MySQL-python-1.2.3/

python setup.py install

 

Once you have completed all these simple tasks you will have a system or environment with a mysql connectable python. Also located in the MySQL-python-1.2.3 folder there will now be a dist/ folder which will contain a nicely packaged egg that you can use for future installs on your system. Which makes the whole process easier to install assuming you picked the route of installing on a virtual environment. Save the egg for later and use it again when you need a new install in a new environment.


easy_install MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg

 

Why you should be using VirtualEnv

One of the more annoying things when writing any code is when libraries change. Either deprecating functions, or changing how you expect functions to be handled. In some languages and platforms this isn’t a very large issue, libraries made by large companies seldom change. But now, so many libraries are community driven, updates are happening almost daily. New releases are happening monthly in some cases.

 

There is a solution, VirtualEnv allows you to create an environment for python and all its libraries. This environment is separate from the rest of your system libraries, anything installed is only accessible from within the environment. I am planning on writing a few articles related to VirtualEnv, everything from setting it up, using it, and deploying a website using django with it.

 

Installing VirtualEnv is fairly simple, I am assuming that you already have easy_install installed.


sudo easy_install virtualenv

 

Once easy_install has done its thing, and virtualenv is installed there’s really only 2 main commands you will want to familiarize yourself with. The first command is used for creating a environment, making sure to use distribute (–distribute) for installing new packages and telling virtualenv to not use the systems already installed packages (–no-site-packages). Finally at then end will be the path (full or relative) to the project location.


virtualenv --no-site-packages --distribute ./project/

cd ./project/

 

Now that your environment is setup, all there is to do is enter the environment, this is easily accomplished


source bin/activate

 

From here you will notice that your prompt has changed, adding the project name at the start, anything you install from here is now installed on your environment and will only be accessible from with in. Installing new packages is easily accomplished using the PIP installer.


pip install django

 

To access your environment all you must do is execute a deactivate command anywhere from within the shell


deactivate

 

Although this might seem like a bit more work then your standard work flow, the benefits severely outweigh the extra work involved. The entire environment is portable, meaning you can dump a list of all libraries (including versions) to a file which allows anyone to completely build your environment on there computer without requiring you to upload the entire set of libraries to them. I will cover this later in the series, stay tuned.