There’s a package (Python module) that interfaces a Python Script with a MySQL database back-end. It’s called MySQLdb. For example:
[root@puma ~] python2.4 Python 2.4.3 (#1, Jun 18 2012, 08:55:23) >>> import MySQLdb >>> [root@puma ~] python2.6 Python 2.6.5 (r265:79063, Jun 8 2010, 15:27:41) >>> import MySQLdb >>> [root@puma ~] python2.7 Python 2.7 (r27:82500, Aug 29 2012, 15:28:26) >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdb >>> [root@puma ~]
Oops. So, that last one is a bit of a problem. No problem, thought I. That’s what pip is for:
[root@puma ~] pip-2.7 install MySQLdb Downloading/unpacking MySQLdb Real name of requirement MySQLdb is MySQLdb Could not find any downloads that satisfy the requirement MySQLdb No distributions at all found for MySQLdb Storing complete log in /root/.pip/pip.log [root@puma ~]
Hmm. Not as easy as I hoped. Okay, so, what is the module actually called?
[root@puma ~] pip-2.7 search MySQLdb jaraco.mysql - MySQLDB-compatible MySQL wrapper by Jason R. Coombs mysqldbda - MySQL Database adapter raisin.mysqldb - UNKNOWN sqlShort - A tiny wrapper for the Python modules MySQLdb and Sqlite sqlwitch - sqlwitch offers idiomatic SQL generation on top of MySQLdb. torndb - A lightweight wrapper around MySQLdb. Originally part of the Tornado framework.
Hmm. I’m still not sure. None of those look quite right, though many of them look interesting.
Okay, we know that we have MySQLdb for python2.4 and python2.6, so let’s see where those come from:
[root@puma ~] locate MySQLdb /usr/lib64/python2.4/site-packages/MySQLdb /usr/lib64/python2.4/site-packages/MySQLdb/__init__.py /usr/lib64/python2.4/site-packages/MySQLdb/__init__.pyc /usr/lib64/python2.4/site-packages/MySQLdb/__init__.pyo /usr/lib64/python2.4/site-packages/MySQLdb/connections.py ... /usr/lib64/python2.4/site-packages/MySQLdb/constants/__init__.pyc /usr/lib64/python2.4/site-packages/MySQLdb/constants/__init__.pyo /usr/share/doc/MySQL-python-1.2.3/MySQLdb.txt [root@puma ~]
Grrr. That’s distressing. So there’s no file/directory called MySQLdb except for Python2.4 So, where is the one for Python2.6? Back to the drawing board:
[cs304@puma ps2] python2.6 Python 2.6.5 (r265:79063, Jun 8 2010, 15:27:41) >>> import sys >>> sys.path ['', '/usr/local/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/usr/local/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/site-packages/Theano-0.3.0-py2.6.egg', ..., '/usr/local/lib/python2.6/site-packages'] >>> import MySQLdb >>> [cs304@puma ps2] cd /usr/local/lib/python2.6/site-packages/ [cs304@puma site-packages] file MySQL_python-1.2.3-py2.6-linux-x86_64.egg MySQL_python-1.2.3-py2.6-linux-x86_64.egg: Zip archive data, at least v2.0 to extract [cs304@puma site-packages] zipinfo MySQL_python-1.2.3-py2.6-linux-x86_64.egg Archive: MySQL_python-1.2.3-py2.6-linux-x86_64.egg 107724 37 -rw-rw---- 2.0 unx 524 b- defN 28-Feb-11 13:37 _mysql.pyc -rw-rw---- 2.0 unx 276 b- defN 28-Feb-11 13:37 _mysql.py -rwxrwx--- 2.0 unx 147488 b- defN 28-Feb-11 13:34 _mysql.so -rw-rw---- 2.0 unx 2306 b- defN 17-Jun-10 03:21 _mysql_exceptions.py -rw-rw---- 2.0 unx 4139 b- defN 28-Feb-11 13:37 _mysql_exceptions.pyc -rw-rw---- 2.0 unx 3121 b- defN 17-Jun-10 03:21 MySQLdb/__init__.py -rw-rw---- 2.0 unx 6317 b- defN 28-Feb-11 13:37 MySQLdb/converters.pyc -rw-rw---- 2.0 unx 12338 b- defN 28-Feb-11 13:37 MySQLdb/connections.pyc -rw-rw---- 2.0 unx 4529 b- defN 28-Feb-11 13:37 MySQLdb/times.pyc ... EGG-INFO/native_libs.txt -rw-rw-r-- 2.0 unx 868 b- defN 28-Feb-11 13:37 EGG-INFO/SOURCES.txt -rw-rw---- 2.0 unx 1 b- defN 28-Feb-11 13:37 EGG-INFO/zip-safe -rw-rw-r-- 2.0 unx 1751 b- defN 28-Feb-11 13:37 EGG-INFO/PKG-INFO -rw-rw-r-- 2.0 unx 33 b- defN 28-Feb-11 13:37 EGG-INFO/top_level.txt 37 files, 279214 bytes uncompressed, 103268 bytes compressed: 63.0% scottas-imac91:~ scotta$
Wow, that was quite a journey. So, it seems the MySQLdb module is inside an “egg” called MySQL_python. Can we find *that* with pip?
Okay, we can, and we were able to upgrade it.
[root@puma cgi-bin] pip-2.6 search MySQL_python MySQL-python - Python interface to MySQL INSTALLED: 1.2.3 LATEST: 1.2.4b5 [root@puma cgi-bin] pip-2.6 install --upgrade MySQL_python Downloading/unpacking MySQL-python from http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.4b5.tar.gz#md5=2d760ee948aff4f50d01afdf8afff48c Downloading MySQL-python-1.2.4b5.tar.gz (82Kb): 82Kb downloaded Running setup.py egg_info for package MySQL-python Downloading Installing collected packages: MySQL-python Found existing installation: MySQL-python 1.2.3 Uninstalling MySQL-python: Successfully uninstalled MySQL-python Running setup.py install for MySQL-python Successfully installed MySQL-python Cleaning up... [root@puma cgi-bin]
One quick test:
[root@puma ~] python2.6 Python 2.6.5 (r265:79063, Jun 8 2010, 15:27:41) [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> [root@puma ~] su - anderson [anderson@puma ~] python2.6 Python 2.6.5 (r265:79063, Jun 8 2010, 15:27:41) [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdb >>> [anderson@puma ~] logout [root@puma ~] ls -lt /usr/local/lib/python2.6/site-packages/ total 732 drwxrwx--- 3 root root 4096 Oct 11 21:45 MySQLdb -rw-rw-r-- 1 root root 4305 Oct 11 21:45 _mysql_exceptions.pyc drwxrwxr-x 2 root root 4096 Oct 11 21:45 MySQL_python-1.2.4b5-py2.6.egg-info -rwxrwxr-- 1 root root 148882 Oct 11 21:45 _mysql.so -rw-rw-r-- 1 root root 445 Oct 11 21:45 easy-install.pth -rw-rw-r-- 1 root root 2352 Oct 11 21:45 _mysql_exceptions.py drwxrwxr-x 4 root root 4096 Jul 26 20:57 pip-1.1-py2.6.egg drwxrwxr-x 5 root root 4096 Jun 17 15:00 networkx-1.7rc1-py2.6.egg drwxrwxr-x 4 root root 4096 Jun 17 14:03 Orange-2.5a4-py2.6-linux-x86_64.egg
Darn! pip never sets the directory permissions properly, and I always forget. (Oh, and I see that the upgrade has created a MySQLdb directory, instead of hiding the module inside an egg.) A quick fix:
[root@puma ~] opendir /usr/local/lib/python2.6/site-packages/
And we’re done:
[anderson@puma ~] python2.6 -c 'import MySQLdb'
whew! Now, can we finally install MySQLdb for python2.7?
[root@puma ~] pip-2.7 search MySQL_python MySQL-python - Python interface to MySQL [root@puma ~] pip-2.7 install MySQL_python Downloading/unpacking MySQL-python Downloading MySQL-python-1.2.4b5.tar.gz (82Kb): 82Kb downloaded Running setup.py egg_info for package MySQL-python Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz Extracting in /tmp/tmpGemlI5 Now working in /tmp/tmpGemlI5/distribute-0.6.28 Building a Distribute egg in /root/build/MySQL-python /root/build/MySQL-python/distribute-0.6.28-py2.7.egg Installing collected packages: MySQL-python Running setup.py install for MySQL-python building '_mysql' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,4,'beta',5) -D__version__=1.2.4b5 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lssl -lcrypto -o build/lib.linux-x86_64-2.7/_mysql.so Successfully installed MySQL-python Cleaning up... [root@puma ~] ls -lt /usr/local/lib/python2.7/site-packages/ | head total 592 drwxrwx--- 3 root root 4096 Oct 11 22:36 MySQLdb -rw-rw---- 1 root root 4303 Oct 11 22:36 _mysql_exceptions.pyc drwxrwx--- 2 root root 4096 Oct 11 22:36 MySQL_python-1.2.4b5-py2.7.egg-info -rwxrwx--- 1 root root 148898 Oct 11 22:36 _mysql.so -rw-rw---- 1 root root 2352 Oct 11 22:36 _mysql_exceptions.py drwxrwx--- 18 root root 4096 Aug 31 10:27 numpy drwxrwx--- 2 root root 4096 Aug 31 10:27 numpy-1.6.2-py2.7.egg-info drwxrwx--- 2 root root 4096 Aug 31 10:23 simplejson-2.6.1-py2.7.egg-info drwxrwx--- 3 root root 4096 Aug 31 10:23 simplejson [root@puma ~] opendir /usr/local/lib/python2.7/site-packages/ [root@puma ~] su - anderson [anderson@puma ~] python2.7 -c 'import MySQLdb' [anderson@puma ~]
Yes, we can! Woo-hoo! (And, as a side-effect, we made numpy and simplejson and other things available in Python2.7.)
Now for Tempest:
[root@tempest ~] pip-2.6 search MySQL_python MySQL-python - Python interface to MySQL [root@tempest ~] pip-2.6 install MySQL_python Downloading/unpacking MySQL-python Downloading MySQL-python-1.2.4b5.tar.gz (82Kb): 82Kb downloaded Running setup.py egg_info for package MySQL-python The required version of distribute (>=0.6.28) is not available, and can't be installed while this script is running. Please install a more recent version first, using 'easy_install -U distribute'. (Currently using distribute 0.6.10 (/usr/lib/python2.6/site-packages)) Complete output from command python setup.py egg_info: The required version of distribute (>=0.6.28) is not available, and can't be installed while this script is running. Please install a more recent version first, using 'easy_install -U distribute'. (Currently using distribute 0.6.10 (/usr/lib/python2.6/site-packages)) ---------------------------------------- Command python setup.py egg_info failed with error code 2 Storing complete log in /root/.pip/pip.log [root@tempest ~] easy_install easy_install easy_install-2.6 easy_install-2.7 [root@tempest ~] easy_install-2.6 -U distribute Searching for distribute Reading http://pypi.python.org/simple/distribute/ Reading http://packages.python.org/distribute Best match: distribute 0.6.28 Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz#md5=b400b532e33f78551e6847c1f5965e56 Processing distribute-0.6.28.tar.gz Running distribute-0.6.28/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ScwCrB/distribute-0.6.28/egg-dist-tmp-1aKoLe Before install bootstrap. Scanning installed packages Setuptools installation detected at /usr/lib/python2.6/site-packages Non-egg installation Removing elements out of the way... Already patched. /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info already patched. After install bootstrap. /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info already exists Adding distribute 0.6.28 to easy-install.pth file Installing easy_install script to /usr/bin Installing easy_install-2.6 script to /usr/bin Installed /usr/lib/python2.6/site-packages/distribute-0.6.28-py2.6.egg Processing dependencies for distribute Finished processing dependencies for distribute [root@tempest ~] pip-2.6 install MySQL_python Downloading/unpacking MySQL-python Running setup.py egg_info for package MySQL-python Installing collected packages: MySQL-python Running setup.py install for MySQL-python building '_mysql' extension gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,4,'beta',5) -D__version__=1.2.4b5 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX In file included from /usr/include/mysql/my_config.h:14, from _mysql.c:44: /usr/include/mysql/my_config_x86_64.h:1082:1: warning: "HAVE_WCSCOLL" redefined In file included from /usr/include/python2.6/pyconfig.h:6, from /usr/include/python2.6/Python.h:8, from _mysql.c:29: /usr/include/python2.6/pyconfig-64.h:808:1: warning: this is the location of the previous definition gcc -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lssl -lcrypto -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so Successfully installed MySQL-python Cleaning up... [root@tempest ~]
Okay, a bit of a detour in the middle, there, but ultimately successful. Wasn’t it? Let’s check:
[root@tempest ~] python2.6 Python 2.6.6 (r266:84292, Aug 28 2012, 10:55:56) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> MySQLdb.__file__ '/usr/lib64/python2.6/site-packages/MySQLdb/__init__.pyc' >>> [root@tempest ~] ls -lt /usr/lib64/python2.6/site-packages/ | head total 8988 drwxrwx---. 2 root root 4096 Oct 11 22:42 MySQL_python-1.2.4b5-py2.6.egg-info -rw-rw----. 1 root root 4257 Oct 11 22:42 _mysql_exceptions.pyc drwxrwx---. 3 root root 4096 Oct 11 22:42 MySQLdb -rwxrwx---. 1 root root 141945 Oct 11 22:42 _mysql.so -rw-rw----. 1 root root 2352 Oct 11 22:40 _mysql_exceptions.py drwxr-xr-x. 2 root root 4096 Oct 3 23:20 qmf -rwxr-xr-x. 1 root root 46472 Sep 11 10:10 SpiceClientGtk.so -rw-r--r--. 2 root root 11329 Sep 5 04:29 drv_libxml2.pyc -rw-r--r--. 2 root root 11329 Sep 5 04:29 drv_libxml2.pyo [root@tempest ~] opendir /usr/lib64/python2.6/
Okay, so the libraries are in /usr/lib64 instead of /usr/local/lib as on Puma. We should check what other library directories python2.6 is on Tempest:
[root@tempest ~] ls -lt /usr/lib/python2.6/site-packages/ | head total 1228 -rw-r--r--. 1 root root 30 Oct 11 22:42 setuptools.pth -rw-rw-r--. 1 root root 457 Oct 11 22:42 easy-install.pth drwxrwx---. 4 root root 4096 Oct 11 22:42 distribute-0.6.28-py2.6.egg drwxr-xr-x. 2 root root 4096 Oct 3 23:20 mllib drwxr-xr-x. 5 root root 4096 Oct 3 23:20 qpid [root@tempest ~] opendir /usr/lib/python2.6/site-packages/
So, we’ve opened that up, too. One last test:
[root@tempest ~] su - anderson [anderson@tempest ~] python2.6 -c 'import MySQLdb'
Now to install MySQLdb for python2.7:
[root@tempest ~] pip-2.7 search MySQL_python MySQL-python - Python interface to MySQL [root@tempest ~] pip-2.7 install MySQL_python Downloading/unpacking MySQL-python Downloading MySQL-python-1.2.4b5.tar.gz (82Kb): 82Kb downloaded Running setup.py egg_info for package MySQL-python Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz Extracting in /tmp/tmpwuvghL Now working in /tmp/tmpwuvghL/distribute-0.6.28 Building a Distribute egg in /root/build/MySQL-python /root/build/MySQL-python/distribute-0.6.28-py2.7.egg Installing collected packages: MySQL-python Running setup.py install for MySQL-python building '_mysql' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,4,'beta',5) -D__version__=1.2.4b5 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX In file included from /usr/include/mysql/my_config.h:14, from _mysql.c:44: /usr/include/mysql/my_config_x86_64.h:1082:1: warning: "HAVE_WCSCOLL" redefined In file included from /usr/local/include/python2.7/Python.h:8, from _mysql.c:29: /usr/local/include/python2.7/pyconfig.h:881:1: warning: this is the location of the previous definition gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lssl -lcrypto -o build/lib.linux-x86_64-2.7/_mysql.so Successfully installed MySQL-python Cleaning up... [root@tempest ~] opendir /usr/local/lib lib/ lib64/ libexec/ [root@tempest ~] opendir /usr/local/lib/python2.7/ [root@tempest ~] su - anderson [anderson@tempest ~] python2.7 -c 'import MySQLdb' [anderson@tempest ~]
Whew! I think we’re good now.