尝试导入python模块时的疯狂行为

时间:2013-02-20 11:33:05

标签: python django pip python-module django-tagging

我想使用Apache mod-wsgi部署django webapp时遇到一些问题,我已经将它们追溯到这一行(缺少django-tagging模块):

[Wed Feb 20 13:08:42 2013] [error] [client 172.19.130.50] ImportError: No module named tagging

现在,当我尝试以root身份和apache用户运行pip-python(我使用CENTOS 6)时,这是我的输出:

[root@app1 site-packages]# pip-python freeze | grep tag
django-tagging==0.3.1
django-taggit==0.9.3

已安装标记...

[root@app1 site-packages]# sudo -u apache pip-python freeze | grep tag
django-tagging==0.3.1
django-taggit==0.9.3

Apache用户也这么说!

[root@app1 /]# python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tagging
>>>

Ok root可以导入标记!

[root@app1 /]# sudo -u apache python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tagging
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named tagging
>>>

但是apache用户不能!!!!!我怎样才能做到这一点????

对于django-tagging,我已经检查了/usr/lib/python2.6/site-packages/的所有权利,并且它们与我的其他软件包相同。

更新1: 我不记得我是如何安装django-tagging模块的 - 但可能是root用户,因为apache无法在全局安装模块!

更新2: 这是martijn-pieters建议的输出:

[root@app1 /]# sudo -u apache python -c 'import sys; print sys.path'
['', '/usr/lib64/python2.6/site-packages/Twisted-12.1.0-py2.6-linux-x86_64.egg', '/usr/lib/python2.6/site-packages/django_cas-2.1.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
[root@app1 /]# python -c 'import sys; print sys.path'
['', '/usr/lib64/python2.6/site-packages/Twisted-12.1.0-py2.6-linux-x86_64.egg', '/usr/lib/python2.6/site-packages/django_cas-2.1.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
[root@app1 /]# sudo -u apache head `which pip-python`
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==0.8','console_scripts','pip'
__requires__ = 'pip==0.8'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pip==0.8', 'console_scripts', 'pip')()
    )
[root@app1 /]# head `which pip-python`
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==0.8','console_scripts','pip'
__requires__ = 'pip==0.8'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pip==0.8', 'console_scripts', 'pip')()
    )

完全相同:(

更新3: 是的,apache用户可以加载其他模块:

[root@app1 /]# sudo -u apache python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import twisted
>>> import cairo

更新4:我讨厌自己。问题是访问权限 /usr/lib/python2.6/site-packages/tagging/目录!他们是drw-r - r--(644)而不是正确的drwxr-xr-x(755)所以apache用户无法进入目录:(

现在一切正常,感谢所有的帮助!

1 个答案:

答案 0 :(得分:1)

我遇到过与此行为类似的情况,最后向Django wsgi.py文件添加了要导入的模块的确切路径:

import sys
PACKAGES ='/usr/local/lib/python2.6/dist-packages/'
sys.path.append(PACKAGES + 'django_compressor-1.1.1-py2.6.egg')

希望它有所帮助。

谁应该受到指责?不确定,也许是apache或者mod_wsgi。在Ubuntu 10.04的apache2中发生了这种情况。来自Debian stable的apache2中的最近设置,并且不再需要sys.path.append进行测试。