ImportError: No module named MySQLdb

354 views
Skip to first unread message

Joao Simoes

unread,
Feb 2, 2016, 12:11:41 PM2/2/16
to WebIOPi
Dear all,

I am trying to get data from a MySQL database from the Webiopi script.py but I get this error all the time: ImportError: No module named MySQLdb

From my Webiopi webpage, when I click a button, I need to get a value from the database.

My python script starts with this instruction: "import MySQLdb as mdb"
Further down, I have a Webiopi macro with following code:

@webiopi.macro
def getTankLevel():
con = mdb.connect("db_ip_address", "username", "password", "database")
curs=con.cursor()
try:
curs.execute ("""SELECT field1 FROM table ORDER BY id DESC LIMIT 1""")
except:
con.rollback()
for rows in curs.fetchall():
outStr = str(rows[0])
con.close()

return "%s" %(outStr)

Could you help me explaining if this is possible to do and if so, how can I do it?

Thank you in advance.
regards 

Joao Simoes

unread,
Feb 4, 2016, 3:23:34 AM2/4/16
to WebIOPi
Dear all,

After some investigation in this forum, I've discovered that webiopi was using python3 which apparently doesn't work so well with MySQLdb.
As such, I've modified the files below, replacing 'python3' by 'python':

/usr/bin/webiopi      (line 2: python -m webiopi $*)
/etc/init.d/webiopi   (line 20: DAEMON=/usr/bin/python)

Thanks to Robert More for his post.

Hope this can help others having the same problem :-)

regards
Joao

Adam S

unread,
Feb 8, 2016, 12:45:05 PM2/8/16
to WebIOPi
The best route would be to use an updated MySQL module than use an outdated one.  Here are some options for you : http://stackoverflow.com/questions/4960048/python-3-and-mysql

Juan Vivas

unread,
Sep 28, 2016, 4:36:00 PM9/28/16
to WebIOPi
You are a star Joao, it works perfect, Many thanks for your post
Reply all
Reply to author
Forward
0 new messages