7 min read

 

MySQL for Python

MySQL for Python

Integrate the flexibility of Python and the power of MySQL to boost the productivity of your Python applications

  • Implement the outstanding features of Python’s MySQL library to their full potential
  • See how to make MySQL take the processing burden from your programs
  • Learn how to employ Python with MySQL to power your websites and desktop applications
  • Apply your knowledge of MySQL and Python to real-world problems instead of hypothetical scenarios
  • A manual packed with step-by-step exercises to integrate your Python applications with the MySQL database server

Getting MySQL for Python

How you get MySQL for Python depends on your operating system and the level of authorization you have on it. In the following subsections, we walk through the common operating systems and see how to get MySQL for Python on each.

Using a package manager (only on Linux)

Package managers are used regularly on Linux, but none come by default with Macintosh and Windows installations. So users of those systems can skip this section.

A package manager takes care of downloading, unpacking, installing, and configuring new software for you. In order to use one to install software on your Linux installation, you will need administrative privileges.

Administrative privileges on a Linux system can be obtained legitimately in one of the following three ways:

  • Log into the system as the root user (not recommended)
  • Switch user to the root user using su
  • Use sudo to execute a single command as the root user

The first two require knowledge of the root user’s password. Logging into a system directly as the root user is not recommended due to the fact that there is no indication in the system logs as to who used the root account. Logging in as a normal user and then switching to root using su is better because it keeps an account of who did what on the machine and when. Either way, if you access the root account, you must be very careful because small mistakes can have major consequences. Unlike other operating systems, Linux assumes that you know what you are doing if you access the root account and will not stop you from going so far as deleting every file on the hard drive.

Unless you are familiar with Linux system administration, it is far better, safer, and more secure to prefix the sudo command to the package manager call. This will give you the benefit of restricting use of administrator-level authority to a single command. The chances of catastrophic mistakes are therefore mitigated to a great degree.

More information on any of these commands is available by prefacing either man or info before any of the preceding commands (su, sudo).

Which package manager you use depends on which of the two mainstream package management systems your distribution uses. Users of RedHat or Fedora, SUSE, or Mandriva will use the RPM Package Manager (RPM) system. Users of Debian, Ubuntu, and other Debian-derivatives will use the apt suite of tools available for Debian installations. Each package is discussed in the following:

Using RPMs and yum

If you use SUSE, RedHat, or Fedora, the operating system comes with the yum package manager . You can see if MySQLdb is known to the system by running a search (here using sudo):

sudo yum search mysqldb

If yum returns a hit, you can then install MySQL for Python with the following command:

sudo yum install mysqldb

Using RPMs and urpm

If you use Mandriva, you will need to use the urpm package manager in a similar fashion. To search use urpmq:

sudo urpmq mysqldb

And to install use urpmi:

sudo urpmi mysqldb

Using apt tools on Debian-like systems

Whether you run a version of Ubuntu, Xandros, or Debian, you will have access to aptitude, the default Debian package manager. Using sudo we can search for MySQLdb in the apt sources using the following command:

sudo aptitude search mysqldb

On most Debian-based distributions, MySQL for Python is listed as python-mysqldb.

Once you have found how apt references MySQL for Python, you can install it using the following code:

sudo aptitude install python-mysqldb

Using a package manager automates the entire process so you can move to the section Importing MySQL for Python.

Using an installer for Windows

Windows users will need to use the older 1.2.2 version of MySQL for Python. Using a web browser, go to the following link:

http://sourceforge.net/projects/mysql-python/files/

This page offers a listing of all available files for all platforms. At the end of the file listing, find mysql-python and click on it. The listing will unfold to show folders containing versions of MySQL for Python back to 0.9.1. The version we want is 1.2.2.

Windows binaries do not currently exist for the 1.2.3 version of MySQL for Python. To get them, you would need to install a C compiler on your Windows installation and compile the binary from source.

Click on 1.2.2 and unfold the file listing. As you will see, the Windows binaries are differentiated by Python version—both 2.4 and 2.5 are supported. Choose the one that matches your Python installation and download it. Note that all available binaries are for 32-bit Windows installations, not 64-bit.

After downloading the binary, installation is a simple matter of double-clicking the installation EXE file and following the dialogue. Once the installation is complete, the module is ready for use. So go to the section Importing MySQL for Python.

Using an egg file

One of the easiest ways to obtain MySQL for Python is as an egg file, and it is best to use one of those files if you can. Several advantages can be gained from working with egg files such as:

  • They can include metadata about the package, including its dependencies
  • They allow for the use of egg-aware software, a helpful level of abstraction
  • Eggs can, technically, be placed on the Python executable path and used without unpacking
  • They save the user from installing packages for which they do not have the appropriate version of software
  • They are so portable that they can be used to extend the functionality of third-party applications

Installing egg handling software

One of the best known egg utilities—Easy Install, is available from the PEAK Developers’ Center at http://peak.telecommunity.com/DevCenter/EasyInstall. How you install it depends on your operating system and whether you have package management software available. In the following section, we look at several ways to install Easy Install on the most common systems.

Using a package manager (Linux)

On Ubuntu you can try the following to install the easy_install tool (if not available already):

shell> sudo aptitude install python-setuptools

On RedHat or CentOS you can try using the yum package manager:

shell> sudo yum install python-setuptools

On Mandriva use urpmi:

shell> sudo urpmi python-setuptools

You must have administrator privileges to do the installations just mentioned.

Without a package manager (Mac, Linux)

If you do not have access to a Linux package manager, but nonetheless have a Unix variant as your operating system (for example, Mac OS X), you can install Python’s setuptools manually. Go to:

http://pypi.python.org/pypi/setuptools#files

Download the relevant egg file for your Python version.

When the file is downloaded, open a terminal and change to the download directory. From there you can run the egg file as a shell script. For Python 2.5, the command would look like this:

sh setuptools-0.6c11-py2.5.egg

This will install several files, but the most important one for our purposes is easy_install, usually located in /usr/bin.

On Microsoft Windows

On Windows, one can download the setuptools suite from the following URL:

http://pypi.python.org/pypi/setuptools#files

From the list located there, select the most appropriate Windows executable file.

Once the download is completed, double-click the installation file and proceed through the dialogue. The installation process will set up several programs, but the one important for our purposes is easy_install.exe. Where this is located will differ by installation and may require using the search function from the Start Menu.

On 64-bit Windows, for example, it may be in the Program Files (x86) directory. If in doubt, do a search. On Windows XP with Python 2.5, it is located here:

C:Python25Scriptseasy_install.exe

Note that you may need administrator privileges to perform this installation. Otherwise, you will need to install the software for your own use. Depending on the setup of your system, this may not always work.

Installing software on Windows for your own use requires the following steps:

  1. Copy the setuptools installation file to your Desktop.
  2. Right-click on it and choose the runas option.
  3. Enter the name of the user who has enough rights to install it (presumably yourself)

After the software has been installed, ensure that you know the location of the easy_install.exe file. You will need it to install MySQL for Python.

LEAVE A REPLY

Please enter your comment!
Please enter your name here