本文介绍了ImportError:没有模块名称MySQLdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我希望有人可以帮我解决以下问题... 谢谢, Fred 我的环境: -------------------------- Slackware Linux 10.2 Python 2.4.2 MySql版本4.1.14 MySql-Python 1.2.0 我想做的是: --------------------------- 使用MySQL,Python,My-Sql-Python模块和CGI创建一个简单的 数据库,可以通过网页加入。 一切都很顺利尝试加载 网页时出现此错误: ImportError:没有模块名称MySQLdb 请注意我这样做通常从 Python运行脚本时不会收到错误。 我读了很多但似乎无法解决问题,我是 认为路径错误。 从命令行启动时,此脚本工作正常: -------- ------------ ------------------------------------------ #!/ usr / bin / python 导入MySQLdb 导入cgi host =''192.168.0.112'' db =''电话'' db = MySQLdb.connect(host ='''192.168.0.112'',db =''phone'') cursor = db.cursor() cursor.execute(" Select * from phone") result = cursor.fetchall()结果记录: 打印记录[0],记录[1],记录[2] 我用这段代码创建了一个网页: ------------------------------------ < html> < form action =" cgi-bin / mysqld_script_test.py"> < input type =" submit"> < / form> < / html> ------------------ ------------------ 网页提交按钮加载下面的脚本: 如果我尝试下面的操作,我的Apache错误日志文件中会出现ImportError:No module name MySQLdb ----------------------------------------------- ---------------------------------- #mysqld_script_test.py #!/ usr / bin / python 导入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() 记录结果: 打印记录[0],记录[1],记录[2]I hope someone can help me with the below problem...Thanks,FredMy enviroment:--------------------------Slackware Linux 10.2Python 2.4.2MySql version 4.1.14MySql-Python 1.2.0What I am trying to do:---------------------------Using MySQL, Python, My-Sql-Python module and CGI create a simpledatabase that can be accesed with a webpage.Everything worked great up to this error when trying to load thewebpage:"ImportError: No module name MySQLdb"Note that I do NOT get the error when running the script normally fromPython.I have read much but can not seem to nail the problem down, I amthinking a path error.This script works perfect when launched from the command line:--------------------------------------------------------------#!/usr/bin/pythonimport MySQLdbimport cgihost = ''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/pythonimport MySQLdbimport cgiprint "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]推荐答案 +1 在开头添加这个你的脚本,*之前*试图导入 其他任何东西。 import sys print" sys.path is" ,sys.path (剪码) - bruno desthuilliers python - cprint'@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')])for p in''o **** @ xiludom.gro''。split(''''')])"+1add this at the beginning of your script, *before* trying to importanything else.import sysprint "sys.path is", sys.path(snip code)--bruno desthuillierspython -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) forp in ''o****@xiludom.gro''.split(''@'')])" 我不愿意提供看似简单的解决方案:是网络 浏览器和服务器在同一台计算机上运行? 通常当你安装MySQLdb时它会安装在 网站内 - 包目录,因此脚本可用,无论是从命令行还是网络CGI脚本运行它们。 因此我怀疑你需要重复安装MySQLdb 您的服务器机器。 问候 Steve - Steve Holden +44 150 684 7255 +1 800 494 3119 暂挂en Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/I hesitate to offer what seems like a simplistic solution: are the webbrowser and the server running on the same computer?Normally when you install MySQLdb it will install inside thesite-packages directory, and so would be available to scripts whetherthey were run from a command line or a web CGI script.I therefore suspect you need to repeat the installation of MySQLdb onyour server machine.regardsSteve--Steve Holden +44 150 684 7255 +1 800 494 3119Holden Web LLC www.holdenweb.comPyCon TX 2006 www.python.org/pycon/ 这篇关于ImportError:没有模块名称MySQLdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 18:00