0

I have a Django app which runs fine on localhost. But when I try it on the server, it gives me the following error (as seen in DEBUG output):

No module named views

Request Method:     GET
Request URL:    url_to_site/accounts/login
Django Version:     1.6.1
Exception Type:     ImportError
Exception Value:    

No module named views

Exception Location:     /opt/dev/site/smsmessages/views.py in <module>, line 1
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/opt/dev/myfolder/project/app',
 '/opt/dev/myfolder/project',
 '/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/lib64/python2.6/site-packages/gtk-2.0',
 '/usr/lib/python2.6/site-packages']

Part of he stack trace is as follow:

/opt/dev/myfolder/project/app/urls.py in <module>
    url(r'^messages/', include('smsmessages.urls', namespace='smsmessages', app_name='smsmessages')),

[some more stack trace output here...]

/opt/dev/myfolder/project/smsmessages/urls.py in <module>
    from smsmessages.views import MessageCreateView, MessageListView, MessageUpdateView, MessageDeleteView

[...]

/opt/dev/myfolder/project/smsmessages/views.py in <module>
    from braces.views import LoginRequiredMixin 

I use Django-braces in this app. But it baffles me that the app works without any glitch when I run:

$ python manage.py runserver

on the same server and:

$ curl mysite
<site's content is returned here without a glitch>

Importing braces.views also seems to work when I do:

$ python manage.py shell
    (InteractiveConsole)
    >>> from braces.views import *
    >>> print "no problem here"

It is only when I go back to Apache as server, then I face the aforementioned error (even with curl).

This makes me wonder if it's either wsgi.py or/and Apache ('2.2.15 (Red Hat)') config that is/are causing the problem. Part of the meta info returned by DEBUG message regarding wsgi and Apache is as follow.

mod_wsgi.listener_port  '80'
mod_wsgi.listener_host  ''
SERVER_SOFTWARE     'Apache/2.2.15 (Red Hat)'
SCRIPT_NAME       u''
mod_ssl.var_lookup  ''
mod_wsgi.handler_script     ''
SERVER_SIGNATURE    '<address>Apache/2.2.15 (Red Hat) Server at site.com Port 80</address>\n'
REQUEST_METHOD  'GET'
PATH_INFO   u'/accounts/login'
SERVER_PROTOCOL     'HTTP/1.1'
mod_wsgi.request_handler    'wsgi-script'
wsgi.url_scheme     'http'
PATH_TRANSLATED     '/opt/dev/myfolder/project/app/wsgi.py/accounts/login'
wsgi.multiprocess   True
mod_wsgi.input_chunked  '0'
DOCUMENT_ROOT   '/var/www/html'
mod_wsgi.process_group  ''
SCRIPT_FILENAME     '/opt/dev/myfolder/project/app/wsgi.py'

The content of wsgi.py is:

import os, sys
sys.path.append('/usr/lib64/python2.6/site-packages/')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Apache configs are as follows:

WSGIPythonPath /opt/dev/myfolder/project/

<Directory /opt/dev/myfolder/project/app>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

<VirtualHost *:80>
Alias /static/ /opt/dev/myfolder/app/static/
ServerAdmin email@email.com
ServerName site.com
ErrorLog logs/site.com-error_log
CustomLog logs/site.com-access_log common
WSGIScriptAlias / /opt/dev/myfolder/project/app/wsgi.py

Is there anything that I am doing wrong or missing? Has anyone seen this error? I've explored everything I could (including the ones on StackOverflow regarding No module named views, but I would really appreciate if somebody who's experienced in Django, Apache, wsgi and probably Django-braces could tell me what might be wrong here. Thank you very much.

user1330974
  • 2,500
  • 5
  • 32
  • 60
  • /opt/dev/site/smsmessages/views.py in , line 1, there seems to be the error, so please show your views.py. – Jingo Feb 18 '14 at 19:56
  • Hi @Jingo, it is quite a big file, but I have pasted it here http://pastebin.com/GHeZcSKf Please let me know if you see any error. Thank you. – user1330974 Feb 18 '14 at 20:13
  • Did you ever figure this out? I'm curious about what the issue was… – Joshua Pokotilow Feb 21 '14 at 00:24
  • Hi @JoshuaPokotilow, yes after a week of poking around the internet and getting help from an experienced sys admin, I found that the cause of the problem is SELinux's access control. When I do "setenforce 0", then it works. That led me to reconfigure access to python and django libraries that I installed in my app. Thanks for your help again. :) – user1330974 Mar 07 '14 at 19:13

1 Answers1

1

I don't have any experience with django-braces, but I'm assuming this is a problem with circular dependencies. I've seen problems with circular dependencies that crop up with mod_wsgi / Apache and not the Django dev server since modules may be imported differently by Django (in different orders) depending on how your site is configured or deployed.

This sounds very similar to Baffled: Django "could not import app.views" but can import app, in WSGI?, for example.

Community
  • 1
  • 1
Joshua Pokotilow
  • 1,213
  • 9
  • 13
  • Thanks, @Joshua. It sounds very much like it. I'll give this blog mentioned in that StackOverflow question and post updates if progress is made. Anyone else who might be reading this, please keep pouring your thoughts on this. I'd like to beyond this problem so that I can move on with testing the rest of the application. Thank you again. – user1330974 Feb 18 '14 at 20:18
  • Np. Usually with circular imports, you can eyeball the backtrace to see if the module that fails to import is referenced further up in the stack. That said, I've encountered cases where it's not so obvious what's going on because a problematic module is being imported dynamically. – Joshua Pokotilow Feb 18 '14 at 21:09