How To use BIBUS With MySQL – 06/29/2005 – Claude PAOLI

To access MySQL databases, BIBUS needs MySQL for Python (previously MySQLdb). The project and downloads can be found at sourceforge : http://sourceforge.net/projects/mysql-python.

Depending on MySQL server versions you want to connect you can typicaly use :

Note :  take care of the Python and MySQL versions used to buid the installation package you could download. If you don't find the correct combination (MySQL version used to build can be the same or higher than the MySQL server you want to access, but Python version must be exaclty the same), you have to build your own package.

Compiling MySQL for Python version 1.2.1c3 :

This version is set as unstable so there is no compiled version available, and even for stable versions not all OS/Python combinations are.

Note : This procedure describes my experience with the 1.2.1c3 version, but I assume that it will be approximately the same for other versions.

For all platforms

You obviously have to install Python on your system (I used Python 2.4 for my tests on Windows 2000 and Python 2.3 with Mandrake Linux 10.1). Then, you have to download the MySQL-python source from SOURCEFORGE, and expand this file to a directory that will be named MySQL-Python-1.2.1c3 in this document. You also need to have the correct MySQL development package installed. For this version of python-MySQL, the 4.1.x is needed At this point, the procedure will begins to be platform dependant.

For Linux Users

Download and install the MySQL Libraries and header file from MySQL Download (I used the Linux x86 RPM package : MySQL-devel-4.1.12.1-i386.rpm). Note that this will not interferes with previous versions of MySQL server installed, only other versions of the development packages have to be removed (I compiled with a MySQL server 4.0 installed and running). Be sure to have installed the C compiler packages (I used the GCC packages provided in standard with my Linux installation). Then open a shell command line and move to the MySQL-Python-1.2.1c3 directory, then you only have to follow the README file. At the prompt type :

$ python setup.py clean
$ python setup.py build
$ python setup.py bdist_rpm

At this point you have a RPM package built in the dist subdirectory of the MySQL-Python-1.2.1c3 directory. You now have to install it and it’s done.

Note : If you only want to install MySQL-Python, you can, with an administrator account, replace the last command line by $ python setup.py install. It will aso work fine, if you previoulsy removed some MySQL-Python potentially installed package.

For Windows 32 users :

It can be much more longer if you don’t have some required tools, but it will finally work (I did it !). 

My procedure have been inspired by the Martin Bless description (http://mail.python.org/pipermail/python-list/2004-December/255184.html) and the Building Python Extensions with the MS Toolkil Compiler page.

First download the MySQL server Windows installation file from MySQL Download (not the Windows Essential !) – Note that you could also download the Without installer and unzip the file to a chosen directory, but the files were approximately the same size, and the install/uninstall programs are fine (I used the V4.1.12-Win32). Before launching the install file, please be sure to have the latest Microsoft Installer on your system (3.0 don’t works with the file I have downloaded). Then launch installation procedure, select a custom installation, and if you only want to compile mysql-Python without running MySQL server, select only the installation of Developer Components (the default is set to install all except this component, you will have to reverse all the default selections). These files will be installed on the C:\Program Files\MySQL\MySQL Server 4.1 directory (if C:\Program Files is your program files default directory). This directory will be assumed as the MySQL server installation directory later in this document. 

We now have to install a C compiler and all needed extensions. We will use the free MS Toolkit C++ compiler, so we have to download it first from http://msdn.microsoft.com/visualc/vctoolkit2003 and then install it. We also have to install the.NET FrameWork Version 1.1 (it is already installed on Windows XP, but Windows 2000 user will find an install program at http://www.microsoft.com/downloads/details.aspx?familyid=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=en – Note that optional Language pack could be needed depending on you language). Then we can install the .Net FrameWork SDK (download at http://www.microsoft.com/downloads/ThankYou.aspx?familyId=9b3a2ca6-3647-4070-9f41-a333c6b9181d&displayLang=en – Note that you will maybe have to select a different download language file download). Some extensions were also needed from the platform SDK, so you have to install it (I used the Windows Server 2003 SP1 Platform SDK downloaded at http://www.microsoft.com/downloads/details.aspx?familyid=EBA0128F-A770-45F1-86F3-7AB010B398A3&displaylang=en). 

The Python 2.4 disutils (used by mysql-Python setup.py to build the package) have not been designed to deal with MS Toolkit Compile, so some changes have to be made to the msvccompiler.py original file. First download the msvccompiler.patch file provided by the Building Python Extensions with the MS Toolkil Compiler page. Then we have to use the patch unix utility that can be found at http://unxutils.sourceforge.net. Launch a command line and move to the Python 2.4 disutils directory (<Python 2.4 install Path> \Lib\distutils) copy the patch.exe program and msvccompiler.patch file to this directory then run > patch.exe msvccompiler.py msvccompiler.patch. We have now to change the detection value for the installed SDK. First check the registry to find the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs Key. Copy the SSID key that looks like 8F9E5EF3-A9A5-491B-A889-C58EFFECE8B3. Then edit the msvccompiler.py file and search for freeSDK = r"SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs\. Then replace the SSID at the end of this line by the one you found in your registry. 

We now have to set some environment variables to be sure that the needed programs and libraries will be found. Note that when you install the SDKs and the MS ToolKit, the installers will propose some of these setting to be made directly to the system. If you choose to, you will not have to set some of the values described below. I have chosen to create a batch file named SetEnv.bat (to be able to compile later versions without so much work) that looks like :

REM You have to change some of the path according to your Python 2.4 and MS SDKs Installation paths
Set PATH=C:\Program Files\Microsoft Visual C++ Toolkit 2003\bin;C:\Program Files\Microsoft SDK\Bin;C:\Program Files\Python2.4.1;%PATH%
Set INCLUDE=C:\Program Files\Microsoft Visual C++ Toolkit 2003\include;C:\Program Files\Microsoft SDK\include;C:\Program Files\Python2.4.1\include;%INCLUDE%
Set LIB=C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\lib;C:\Program Files\Microsoft Visual C++ Toolkit 2003\lib;C:\Program Files\Microsoft SDK\Lib;C:\Program Files\Python2.4.1\libs;%LIB%

REM You have to change the path according to your MySQL Server installation path
set mysqlroot=C:\Program Files\MySQL\MySQL Server 4.1

REM The default mysqlclient used by mysql-Python is set to mysqlclient_r library that doesn’t exist with windows, so use the mysqlclient library
set mysqlclient=mysqlclient

REM Set the lib path to MySQL libraries (the setup.py file tries to but the path seems to be have been changed)
REM You have to change the path according to your MySQL Server installation
Set LIB=C:\Program Files\MySQL\MySQL Server 4.1\lib\opt;%LIB%

Save the created file in the MySQL-Python-1.2.1c3 directory, we will use it later. 

The setup.py file located in the MySQL-Python-1.2.1c3 directory also needs some changes. Edit this file and find the if sys.platform == "win32": line. After this line, add in the if statement add extra_compile_args = "" (with the correct indentation according to python syntax).

HERE WE ARE ! If everything is OK, we can now try to compile and build the install file ! First open a command line and move to the MySQL-Python-1.2.1c3 directory. Then type the commands :

> SetEnv.bat
> python setup.py clean
> python setup.py build
> python setup.py bdist_wininst

If everything goes right, the compilation process provides some warning messages, but you should obtain a MySQL-python.exe-1.2.1c3.win32-py2.4.exe file in the dist subdirectory of the MySQL-Python-1.2.1c3 directory. Just execute this file to install python-MySQL !

Note : If you only want to install MySQL-Python, you can, with an administrator account, replace the last command line by > python setup.py install. It will aso work fine, if you previoulsy removed some MySQL-Python potentially installed package.

For MAC Users

I don’t have such platform so I can’t help. I assume that it will look like the Linux one.