By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

ImportError: No module name MySQLdb

P: n/a
I hope someone can help me with the below problem...

Thanks,
Fred

My enviroment:
--------------------------
Slackware Linux 10.2
Python 2.4.2
MySql version 4.1.14
MySql-Python 1.2.0

What I am trying to do:
---------------------------
Using MySQL, Python, My-Sql-Python module and CGI create a simple
database that can be accesed with a webpage.

Everything worked great up to this error when trying to load the
webpage:
"ImportError: No module name MySQLdb"

Note that I do NOT get the error when running the script normally from
Python.

I have read much but can not seem to nail the problem down, I am
thinking a path error.

This script works perfect when launched from the command line:
--------------------------------------------------------------
#!/usr/bin/python
import MySQLdb
import cgi
host = '192.168.0.112'
db = 'phone'

db=MySQLdb.connect(host = '192.168.0.112', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone")
result = cursor.fetchall()
for record in result:
print record[0],record[1],record[2]
I created a webpage with this code:
------------------------------------
<html>
<form action="cgi-bin/mysqld_script_test.py">
<input type="submit">
</form>
</html>
------------------------------------

The webpage "submit" button loads the below script:
If I try the below I get the "ImportError: No module name MySQLdb"
in my Apache error log file
---------------------------------------------------------------------------------
#mysqld_script_test.py
#!/usr/bin/python

import MySQLdb
import cgi
print "Content-Type: text/html\n"

host = '192.168.0.112'
db = 'phone'

db=MySQLdb.connect(host = '192.168.0.112', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone")
result = cursor.fetchall()
for record in result:
print record[0],record[1],record[2]

Jan 26 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Fred wrote:
I hope someone can help me with the below problem...

Thanks,
Fred

My enviroment:
--------------------------
Slackware Linux 10.2
Python 2.4.2
MySql version 4.1.14
MySql-Python 1.2.0

What I am trying to do:
---------------------------
Using MySQL, Python, My-Sql-Python module and CGI create a simple
database that can be accesed with a webpage.

Everything worked great up to this error when trying to load the
webpage:
"ImportError: No module name MySQLdb"

Note that I do NOT get the error when running the script normally from
Python.
So I'd say it has something to do with sys.path

Is there anything "non-standard" with your config ? Like modules
installed as a user and/or outside of /usr/lib/pythonXXX/site-packages/ ?
I have read much but can not seem to nail the problem down, I am
thinking a path error.


+1

add this at the beginning of your script, *before* trying to import
anything else.

import sys
print "sys.path is", sys.path
(snip code)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jan 26 '06 #2

P: n/a
Fred wrote:
I hope someone can help me with the below problem...

Thanks,
Fred

My enviroment:
--------------------------
Slackware Linux 10.2
Python 2.4.2
MySql version 4.1.14
MySql-Python 1.2.0

What I am trying to do:
---------------------------
Using MySQL, Python, My-Sql-Python module and CGI create a simple
database that can be accesed with a webpage.

Everything worked great up to this error when trying to load the
webpage:
"ImportError: No module name MySQLdb"

I hesitate to offer what seems like a simplistic solution: are the web
browser and the server running on the same computer?

Normally when you install MySQLdb it will install inside the
site-packages directory, and so would be available to scripts whether
they were run from a command line or a web CGI script.

I therefore suspect you need to repeat the installation of MySQLdb on
your server machine.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 26 '06 #3

P: n/a
Re-reading my message I noticed a stupid error, not effecting my
problem but annoying, I assigned variables and then did not use them,
then included import cgi for my regular script. This is how the command
line script should look:

#!/usr/bin/python
import MySQLdb

db=MySQLdb.connect(host = '192.168.0.112', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone")
result = cursor.fetchall()
for record in result:
print record[0],record[1],record[2]

Jan 26 '06 #4

P: n/a
This is the result of print sys.path:
print sys.path

['', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4',
'/usr/local/lib/python2.4/plat-linux2',
'/usr/local/lib/python2.4/lib-tk',
'/usr/local/lib/python2.4/lib-dynload',
'/usr/local/lib/python2.4/site-packages']

MySQLdb lives here but is not in the path:
/usr/local/lib/python2.4/site-packages/MySQLdb

Everything is running on the same machine here in my house, everything
was installed and is launched as root.

Thanks.
Fred

Jan 26 '06 #5

P: n/a
Here is the complete error from my Apache error log:

Traceback (most recent call last):
File "/var/www/cgi-bin/mysqld_script_test.py", line 7, in ?
import MySQLdb
ImportError: No module named MySQLdb
[Thu Jan 26 07:25:16 2006] [error] [client 127.0.0.1] malformed header
from script. Bad header=['/var/www/cgi-bin', '/usr/lib:
/var/www/cgi-bin/mysqld_script_test.py

Jan 26 '06 #6

P: n/a
>From what I can tell everything seems right. Here are the results of
the sys.path:
print sys.path

['', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4',
'/usr/local/lib/python2.4/plat-linux2',
'/usr/local/lib/python2.4/lib-tk',
'/usr/local/lib/python2.4/lib-dynload',
'/usr/local/lib/python2.4/site-packages']

MySQLdb lives here: /usr/local/lib/python2.4/site-packages/MySQLdb but
is not in the path....

I installed and am trying to run everything as root, and it is all on
the same computer right here with me.

If I can ever get this simplistic problem solved maybe you guys can
help me to get Zope running, it is not cooperating either... :-)

Fred

Jan 26 '06 #7

P: n/a
Fred wrote:
Here is the complete error from my Apache error log:

Traceback (most recent call last):
File "/var/www/cgi-bin/mysqld_script_test.py", line 7, in ?
import MySQLdb
ImportError: No module named MySQLdb
[Thu Jan 26 07:25:16 2006] [error] [client 127.0.0.1] malformed header
from script. Bad header=['/var/www/cgi-bin', '/usr/lib:
/var/www/cgi-bin/mysqld_script_test.py

I think you can ignore this error: it's happening because of your debug
output of syst.path, which presumably takes place before the script
prints out

"""Content-Type: text/html

"""
or similar.

If your program doesn't do that it'll likely not produce what you expect
anyway.

Finally, you might want to think about adding

import cgitb; cgitb.enable()

to your existing scripts. This will give you a much more acceptable
traceback from web conditions.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 26 '06 #8

P: n/a
Fred wrote:
Slackware Linux 10.2
Heh, Slackware was my first Linux distro. Version
2.2 I think. 1993 maybe?
Everything worked great up to this error when trying to load the
webpage:
"ImportError: No module name MySQLdb"


Some suggestions:

Start python interactively and try "import MySQLdb".
Stuff in a subdirectory to site-packages are normally
available. If this doesn't work, it seems something
is fishy with your MySQLdb install, provided you
are using it right. There might be an __init__.py
missing or something, but this is a mature product
which ought to have a stable install procedure.

If things work interactively, but not in the CGI
script, you might want to add

import sys
print sys.path

to your CGI script and see what that gives you.
Finally, the cgitb module is pretty useful. I
suggest that you look it up:
http://docs.python.org/lib/module-cgitb.html
Jan 26 '06 #9

P: n/a
Magnus Lycka wrote:
Fred wrote:
Slackware Linux 10.2
Heh, Slackware was my first Linux distro. Version
2.2 I think. 1993 maybe?


I have been using Slackware since 1995, version 3.0 kernel 1.2.13
Some suggestions: Finally, the cgitb module is pretty useful. I
suggest that you look it up:
http://docs.python.org/lib/module-cgitb.html


Will check it out... I bet this is something really simple and possibly
related to my use of Slackware, although hours of searching has not
produced an answer..

Fred

Jan 26 '06 #10

P: n/a
I have discovered the possible problem (I can't check this until later
when I am home)

I think when I upgraded to Python 2.4.2 it created my problem.

Below is what is going on:

Default Slackware 10.2 install - 2.4.1 installed into
/usr/lib/python2.4
Python 2.4.1 upgraded to 2.4.2
Upgrade downloaded from Python.org compiled and installed - 2.4.2
installed into /usr/local/lib/python2.4

Bin files are in: /usr/bin/python and /usr/local/bin/python

MySQLdb instatlled after the 2.4.2 upgrade works fine at the command
line

I am betting that when I launch the script from the webpage it is
looking for /usr/bin/python

I should be able to make a symlink in /usr/bin/python -->
/usr/local/bin/python

And of course change #!/usr/bin/python to #!/usr/local/bin/python in
the script

What do you think?
Fred

Jan 26 '06 #11

P: n/a
Fixed...

All I did was change #!/usr/bin/python to #!/usr/local/bin/python

The page loaded right up.... it was loading Python ver 2.4.1 rather
than 2.4.2 where the MySQL db module was installed...

I knew it would be something easy... I learned something so it was
worth it...

Jan 27 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.