[Zope] MySQLDA and MySQLdb probs and solns

Eric L. Walstad ewalstad@energywright.com
Fri, 31 Mar 2000 22:24:52 -0800


When building MySQLdb:
"ImportError: No module named _mysql"

This little error message plagued me for about 3 weeks.  After reformatting
my hard drive and re-installing Linux, Python, MySQL and MySQLdb, all from
the source files, I finally found the solution (well, it worked for me) and
am posting it for anyone else that runs into the same trouble.  The good
news (for you) is that all the re-installing isn't necessary.
Note that this solution is for a problem I had in following the "How-To
ZMySQLDA - from download to connection" step number four.

### SOLUTION (worked for me)###
In order to fix the "ImportError: No module named _mysql" error:
Ensure that there exists the /usr/lib/mysql folder containing the following
files:
libdbug.a   libmysqlclient.a   libmysqlclient.so.6      libmysys.a
libheap.a   libmysqlclient.la  libmysqlclient.so.6.0.0  libnisam.a
libmerge.a  libmysqlclient.so  libmystrings.a

<Problem #1>
I had to copy /user/local/lib/mysql to /usr/lib/mysql
</Problem #1>

To fix the "ImportError: libmysqlclient.so.6: cannot open shared object
file: No such file or directory" error:
Ensure that there exists the libmysqlclient.so.6 file in the /usr/lib
folder.

<Problem #2>
The libmysqlclient.so.6 file was missing from the /usr/lib folder.  I tried
putting a link to the libmysqlclient.so.6 file in the /usr/lib folder and it
didn't work.  Next, I copied the libmysqlclient.so.6.0.0 file to the
/usr/lib folder, renamed it to libmysqlclient.so.6 and that was enought to
make the build.py script run all the way through.
</Problem #2>

To successfully complete the installation, I did the following:
[root@frankenstein MySQLdb]# make install
[root@frankenstein MySQLdb]# cp MySQLdb.py{,c,o}
/usr/local/lib/python1.5/site-packages
[root@frankenstein MySQLdb]# cp ZMySQLDA.patch /usr/local/Zope
[root@frankenstein MySQLdb]# cd /usr/local/Zope
[root@frankenstein Zope]# patch -p1<ZMySQLDA.patch
patching file `lib/python/Products/ZMySQLDA/DA.py'
patching file `lib/python/Products/ZMySQLDA/db.py'
Hunk #1 succeeded at 103 with fuzz 2.


### Explanation (what I learned) ###

First, here's the full error message:
gcc -fpic  -I/usr/include/mysql -g -O2 -I/usr/local/include/python1.5 -I/usr
/local/include/python1.5 -DHAVE_CONFIG_H -c ./_mysqlmodule.c
gcc -shared  _mysqlmodule.o  -L/usr/lib/mysql -lmysqlclient -o
_mysqlmodule.so
/usr/i386-linux/bin/ld: cannot open -lmysqlclient: No such file or directory
make: *** [_mysqlmodule.so] Error 1
Traceback (innermost last):
  File "build.py", line 14, in ?
    import MySQLdb
  File "MySQLdb.py", line 19, in ?
    from _mysql import *
ImportError: No module named _mysql

I needed to understand what was happening, so here is what I learned in
breaking this error message down:
<First Command>
gcc -fpic  -I/usr/include/mysql -g -O2 -I/usr/local/include/python1.5 -I/usr
/local/include/python1.5 -DHAVE_CONFIG_H -c ./_mysqlmodule.c
This command is creating an object file (it will be called "_mysqlmodule.o"
by default) from the "_mysqlmodule.c" file.
-fpic = tells the compiler to created a shared library
-fPIC = tells the compiler to create a dynamically linked shard library (not
used here, but good to know)
-c = Tells the compiler to compile only - doesn't create an executable
-g = compile code for debugging (I don't know why we are putting the debug
info in it.)
-O2 = Tells the compiler to use the second level of optimization
-I<path> = Defines path to the header files that are '#include'd in the C
programming code files.
</First Command>

<Second Command>
gcc -shared  _mysqlmodule.o  -L/usr/lib/mysql -lmysqlclient -o
_mysqlmodule.so
This command creates a shared library out of _mysqlmodule.o, called
_mysqlmodule.so, and links it to /usr/lib/mysql/libmysqlclient.so"
-shared = Tells the compiler to place the compiled files into a library
-o<name> = tells the linker to build the '<name>' library
-L<path> = adds <path> to the list of paths to search when looking for the
library to be linked
-l<name> = Name of the library to link to.  Tells the linker to link
the -o<name> program to the 'lib<name>.so' library file
</Second Command>

<First Error>
/usr/i386-linux/bin/ld: cannot open -lmysqlclient: No such file or directory
This error happened because the linker (the ld program) searched for the
"libmysqlclient.so" file and couldn't find it.
</First Error>

<Second Error>
make: *** [_mysqlmodule.so] Error 1
Traceback (innermost last):
  File "build.py", line 14, in ?
    import MySQLdb
  File "MySQLdb.py", line 19, in ?
    from _mysql import *
ImportError: No module named _mysql
This error happened because the first error caused a failure to create the
"_mysqlmodule.so" file.  The "_mysqlmodule.so" library file contains a
module called _mysql.  Because the library file wasn't there, neither was
the _mysql module.
</Second Error>

Next I tried to run the build.py script and received the following error:
<Third Error>
Traceback (innermost last):
  File "build.py", line 14, in ?
    import MySQLdb
  File "MySQLdb.py", line 19, in ?
    from _mysql import *
ImportError: libmysqlclient.so.6: cannot open shared object file: No such
file or directory
This error happened because the "from _mysql import *" statement caused the
"_mysqlmodule.so" library to be loaded, which caused the
"libmysqlclient.so.6" to be loaded, also.  Remember that the command:
"gcc -shared  _mysqlmodule.o  -L/usr/lib/mysql -lmysqlclient -o
_mysqlmodule.so"
(the Second Command, above) linked the "libmysqlclient.so.6" library to the
"_mysqlmodule.so"  that's why they are both being loaded.
</Third Error>

I realize that this problem was likely caused by a missing, or incorrect,
path statement somewhere in my system.  My solution of simply copying files
around is not very elegant, but it worked.  A better solution would've been
to fix the paths.  I don't know how to do that, but when I figure that out,
I'll come back and fix it.

Any comments on this solution are welcomed!

Eric.