3
votes

I have some problems with a django webapp I want to deploy with Apache mod-wsgi and I've traced them down to this line (the django-tagging module is missing):

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

Now, here's my output when I try to run pip-python (I'm using CENTOS 6) as root and as the apache user:

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

Tagging is installed...

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

Apache user says the same !

[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 can import tagging !

[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
>>>

But the apache user cannot !!!!! How can I make this work ????

I've double and triple checked all rights to /usr/lib/python2.6/site-packages/ for django-tagging and they are the same as my other packages.

Update 1: I don't really remember how I did install the django-tagging module - but probably as root since apache cannot install modules globally !

Update 2: Here's the output of martijn-pieters's suggestions:

[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')()
    )

The are exactly the same :(

Update 3: Yes, the apache user can load other modules:

[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

Update 4: I hate myself. The problem was the access rights of the /usr/lib/python2.6/site-packages/tagging/ directory! They were drw-r--r-- (644) instead of the correct drwxr-xr-x(755) so the apache user could not go inside the directory :(

Everything works fine now, thanks for all the help!

1
how did you pip install it? Did you install as user root or as user apache? Installing as different users gives you different access to modules in pip. - Henrik Andersson
compare sudo -u apache python -c 'import sys; print sys.path' with python -c 'import sys; print sys.path', and sudo -u apache head `which pip-python` with head `which pip-python` perhaps? - Martijn Pieters♦
Can you import other modules ? also read surviving nix permissions - jpic
Use virtualenv next time. - wRAR
Do you have SELinux enabled? Check your syslogs for clues. - Austin Phillips

1 Answers

1
votes

I have experienced something similar to this behaviour and ended up adding to Django wsgi.py file the exact path of modules to be imported:

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

Hope it helps.

Who is to blame? not sure, maybe apache or maybe mod_wsgi. This happened to me in an apache2 from Ubuntu 10.04. Recents setups in apache2 from Debian stable and testing never needed to sys.path.append anymore.