ImportError: No module named MySQLdb

2010 April 28 at 11:15 » Tagged as :netbeans, python, certification,

When you see that title, you are gonna say; 'you need to install the MySQLdb module'. Well, let me assure you that it's already it's already there. Provided by MySQL-python.2.3-0.4.c1.fc12.x86_64.rpm . On the other hand if you are here because you ran into the same problem and want a solution, but don't want to listen to my ramble, just scroll down to the bottom to see how it's solved.

All this started when I decided to write a small script to analyze the content of a database using python rather than my usual first choice PHP. But I haven't really coded anything at all with Python since I learned it nearly two years ago. So I thought, why not use Python, well the time I spent researching the cause of this problem could have been utilized to write the darned thing in PHP, Perl or Java or heck even C.

The last time, I did anything with Python, I ran the scripts either from the command line or using mod_python. I can't remember the editor or IDE I was using at the time but I pretty sure it wasn't Netbeans. The problem pops up only when you try to run the script from inside Netbeans itself. The very same script can be executed from the shell using the python interpreter (see, what did I tell you about mysqldb being installed?)

Searching for Import Error: No module named MySQLdb netbeans came up with a blank. Digging a bit deeper, I realized that Netbeans using the jython intepreter rather than the default python 2.6 interpreter that's available on Fedora. That in turn enabled me to dig up a post on Stackoverflow which explained that MySQLdb cannot be used with jython. You need to use something called zxJDBC instead. Damned if I am going to bother with that. Instead I just changed the python platform and solved it easily.

To change the platform, Right Click on the projects root node (the project name in the 'projects tab'), when the dialog pops up select the 'python' item from the tree on the left and then change the platform.

Changing the Python Platform
Changing the Python Platform (click to enlarge)

If by some chance, the installed python interpreter does not show up here, click the manage button and on the new dialog, choose the 'auto-detect' button. If today isn't your lucky day, netbeans will refuse to find your default python interpreter. But that's ok, just run to the command line do a `which python` and then use the path to the interpreter as reported by which to add a 'New' platform.

Adding a new python platform
Adding a new python platform (click to enlarge)

Hopefully you might find my older post ImportError: No module named dom.minidom also to be usefull