web.py没有导入MySQLdb

时间:2011-09-29 19:58:50

标签: python web.py mysql-python sys.path

我正在使用web.py,由于某种原因,我收到以下错误:

[Thu Sep 29 13:47:20 2011] [error] [client 64.8.210.120]   File "/usr/lib/python2.6/site-packages/web.py-0.36-py2.6.egg/web/db.py", line 975, in __init__
[Thu Sep 29 13:47:20 2011] [error] [client 64.8.210.120]     import MySQLdb as db
[Thu Sep 29 13:47:20 2011] [error] [client 64.8.210.120] ImportError: No module named MySQLdb

这是我的sys.path,如果是罪魁祸首?我看到了.egg,所以我不知道。想法?

 ['', '/usr/lib/python2.6/site-packages/Bravo-1.7.2-py2.6.egg', 
    '/usr/lib/python2.6/site-packages/Twisted-11.0.0-py2.6-linux-i686.egg',
     '/usr/lib/python2.6/site-packages/construct-2.04-py2.6.egg',
 '/usr/lib/python2.6/site-packages/zope.interface-3.7.0-py2.6-linux-i686.egg', '/usr/lib/python2.6/site-packages/web.py-0.36-py2.6.egg', 
'/usr/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg', 
    '/usr/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg',
     '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', 
    '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/site-packages',
     '/usr/lib/python2.6/site-packages/PIL']

以下是2.6 site-packages目录的内容:

[root@xxxx ~]# locate *.egg
/root/MySQL-python-1.2.3/dist/MySQL_python-1.2.3-py2.6-linux-i686.egg
/usr/lib/python2.4/site-packages/errorhandler-1.1.1-py2.4.egg
/usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
/usr/lib/python2.4/site-packages/xlrd-0.7.1-py2.4.egg
/usr/lib/python2.4/site-packages/xlutils-1.4.1-py2.4.egg
/usr/lib/python2.4/site-packages/xlutils-1.4.1-py2.5.egg
/usr/lib/python2.4/site-packages/xlwt-0.7.2-py2.4.egg
/usr/lib/python2.6/site-packages/Bravo-1.7.2-py2.6.egg
/usr/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg
/usr/lib/python2.6/site-packages/Twisted-11.0.0-py2.6-linux-i686.egg
/usr/lib/python2.6/site-packages/construct-2.04-py2.6.egg
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg
/usr/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg
/usr/lib/python2.6/site-packages/web.py-0.36-py2.6.egg
/usr/lib/python2.6/site-packages/zope.interface-3.7.0-py2.6-linux-i686.egg

如果我打开python(2.4或2.6),我可以import MySQLdb没问题。

谢谢, 汤姆

3 个答案:

答案 0 :(得分:1)

在bashrc的底部(通常在/etc/bashrc)文件中放置此文件(或只更新PYTHONPATH env变量以包含/usr/lib/python2.6/site-packages):

if [ $PYTHONPATH ]
then
    export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.6/:/usr/lib/python2.6/site-packages/
else
    export PYTHONPATH=/usr/lib/python2.6/:/usr/lib/python2.6/site-packages/
fi

或者,对于正在运行脚本的特定用户,请将上述内容放在/home/[USER_RUNNING_SCRIPT]/.bashrc

然后获取文件:

$ source ~/.bashrc
# or
# source /etc/bashrc

答案 1 :(得分:0)

答案 2 :(得分:0)

尝试编写一个简单的页面,以显示Web服务器执行环境中存在的sys.path。当您在问题中显示sys.path或者从登录中以交互方式运行时,不确定这是否是您正在显示的内容。机会是运行http服务器的用户标识,其环境设置与普通用户不同。