473,226 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

ImportError: No module name MySQLdb

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
11 25566
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
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
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
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
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
>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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olivier Vierlinck | last post by:
Hi, I've a python script using somecalls to the abs() built-in function. Now, I have to import a module named 'abs' too... The consequence if that line code like if (abs(a-b) < epsilon:...
2
by: Casey Hawthorne | last post by:
Is there a way to determine -- when parsing -- if a word contains a builtin name or other imported system module name? Like "iskeyword" determines if a word is a keyword! -- Regards, Casey
1
by: John Pass | last post by:
Hi, This is something really simple. A Console Application example in a course book, where the module file name was renamed to Welcome1, works fine when I use the original name "module1" for the...
3
by: Brahm | last post by:
hey, another question guys.. is there any way to get module name ? E.g: bas_serial_comm.vb Daniel
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
0
by: Ron Adam | last post by:
Is there a function to find a filename from a dotted module (or package) name without importing it? The imp function find_module() doesn't work with dotted file names. And it looks like it may...
0
by: Pradnyesh Sawant | last post by:
Hello, I have a string which in reality is the name of a module to be imported. The name of the class contained in the module also has the same (although with different capitalization). My...
3
by: kwatch | last post by:
What is the condition of module name which is available in 'from .. import ..' statement ? ---------------------------------------- import os print os.path # <module 'posixpath'...
4
by: rlntemp-gng | last post by:
I am trying to dynamically populate the module name in the error message. Currently I populate a variable with hardcoded text: strModuleName = "cmdFlushWorkTables_Click" Per my code below, is...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.