Archive for June, 2011
Python and MySQL dev on OSX
Jun 26th
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
Jun 24th
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.
Times are changing
Jun 23rd
It has been almost a full year since my last post, in that short time much has changed. My previous place of work has decided to pull their offices out of town leaving myself and many others behind. Many sought refuge in a few larger businesses in town and seem to be enjoying it. A few have ventured out into smaller local companies, bringing with them there knowledge and expertise. In January I decided I would do something I have always wanted to do, I incorporated Nullobject and started working on contracts and whatever work I could find in an effort to build up some money into the company as well as keep my bills payed.
I have decided that I want to keep a record of some of the stuff I am working on, as well as continue to help people learn in all areas. I plan on posting articles on things as I did before, problems I tackle and solve, solution to problems people have, as well as the usual rants and opinions. I am hoping that I will learn more by teaching others and others will learn as well. So anyone that can bare my horrible writing and want to learn new stuff together, are welcome to join me. I plan on keeping updates frequent and with substance.
