关于django:尝试导入python模块时的疯狂行为

Crazy behaviour when trying to import python module

我想用Apache mod wsgi部署的django webapp有一些问题,我已经跟踪到这一行(缺少django标记模块):

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

现在,当我尝试运行pip python(我使用centos 6)作为根用户和apache用户时,下面是我的输出:

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

已安装标记…

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

Apache用户也这么说!

1
2
3
4
5
6
[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
>>>

确定根可以导入标记!

1
2
3
4
5
6
7
8
9
[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用户不能!!!!!!!我怎样才能做到这一点?????

我已经仔细检查了/usr/lib/python2.6/site-packages/的所有Django标记权限,它们与我的其他包相同。

更新1:我真的不记得我是如何安装Django标记模块的,但可能是作为根用户,因为Apache无法全局安装模块!

更新2:以下是Martijn Pieters的建议:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[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用户可以加载其他模块:

1
2
3
4
5
6
7
[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)而不是正确的drw xr-xr-x(755),因此apache用户无法进入目录:(

现在一切正常,谢谢你的帮助!


我经历过类似于这种行为的事情,最后将要导入的模块的确切路径添加到django wsgi.py文件中:

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

希望有帮助。

该怪谁?不确定,可能是阿帕奇或者摩德基。这发生在我从Ubuntu10.04的Apache2中。Apache2中Debian稳定版的Recents设置和测试不再需要sys.path.append。