473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 25592
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.