5

I've poured over the site for a while now, and haven't found a resolution that works for me. Long time listener, first time caller.

I have an existing Django application (based on the Django documentation sample site including 'polls'). I've moved this over and got it up and running on a web server via wsgi.

I have created a new local project of my own content. I did so in a manner similar to the sample site with an app on the same level as the project's main application.

I'm now trying to add the app that I created to the sample site. I'm trying to mirror the organization and plumbing for the polls app as it connects to "mysite".

My file structure is as follows:

  • passenger_wsgi.py
  • mysite
    • mysite (main app)
      • __init__.py
      • settings.py
      • urls.py
      • wsgi.py
    • polls (sample site app)
      • __init__.py
      • urls.py
      • views.py
      • models.py
    • core (app that I'm adding)
      • __init__.py
      • urls.py
      • views.py
      • models.py
    • manage.py

The addition to settings looks as follows:

INSTALLED_APPS = {
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'core'
}

mysite/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
    url(r'^$', 'polls.views.home', name='home2'),
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^core/', include('core.urls', namespace="core")),
)

core/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
#from core import views
urlpatterns = patterns('',
    url(r'^', 'core.views.home', name='home'),
)

When I load up "manage.py shell", I can import urls, models, and views all from core. I can even use django.test.Client to "get" "/core/" and I get a 200 response with the expected content. However, when I try to navigate to the site (any url) in a browser, I get: "ImportError at No module named urls"

If I remove the call to include core.urls the other urls work as expected.

I've tried a number if different tweaks to both urls.py files including calling calling core.views directly from the top urls.py ignoring the core.urls which results in "No module named views".

Any advice would be greatly appreciated. I also welcome advice on how to write better questions.

Stack Trace

Edit: Also wanted to mention that I tried deleting all compiled python files to eliminate the possibility of unupdated logic.

Edit 2: It is running in a virtualenv. I've installed a newer version of Python on a Dreamhost server.

passenger_wsgi.py:

import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/mysite')

if sys.version < "2.7.9":
       os.execl(cwd + "/env/bin/python", "python2.7", *sys.argv)

sys.path.insert(0, cwd + '/env/bin')
sys.path.insert(0, cwd + '/env/lib/python2.7')
sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages/django')
sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

mysite/mysite/wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

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

Edit 3: Here's the core/views.py

from django.shortcuts import render
from django.core.mail import send_mail
from django.http import HttpResponseRedirect

# Create your views here.
def home(request):
    return render(request, 'core/home.html')

Edit 4: Sample GET header from browser. Results in 500 Import Error; No module named urls

GET /core/ HTTP/1.1
Host: #redacted
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: csrftoken=36HpckIqzOMbw29BOrQWcnaIJbMn6pwo; sessionid=z9c58vb6610kevo4yytolcbhlaqsoqnm

Edit 5: Since the response has predominantly indicated that things look right, I've expanded the scope of the question to include Dreamhost and submitted a support ticket with them. I will report back here if anything useful comes of that.

Thew
  • 61
  • 9
  • Do you have an empty `__init__.py` in core? – Daniel Roseman Feb 11 '15 at 22:59
  • Good point. Yes, there's an `__init__.py` in the core directory. – Thew Feb 11 '15 at 23:01
  • Hmm. Can't see anything obviously wrong. Can you paste the full traceback? Also note that you don't need to manually create the app structure, `manage.py startapp` will do it for you. – Daniel Roseman Feb 11 '15 at 23:15
  • Here's a [stack trace](http://dpaste.com/02Z5BXA). I did actually create the app using startapp but in a different project. I'm trying to effectively "deploy" the app to another project that's already hosted. – Thew Feb 11 '15 at 23:22
  • Line 39 of the tb shows you have two levels of "mysite" directories. Any chance you're running a different settings.py than you think you are? – dylrei Feb 11 '15 at 23:39
  • I triple checked just to be sure. There's only one settings.py file in the whole of the project. – Thew Feb 11 '15 at 23:44
  • 1
    @Thew Nothing looks wrong in what you have posted here. Also, you're able to run it in the shell, so that's a good sign. My next guess is your WSGI config. Anything unusual in your wsgi.py file? Or your web server wsgi config? Are you running this in a virtualenv? – dylrei Feb 12 '15 at 00:17
  • It is in fact running in a virtualenv. I've included the wsgi files in the original post. – Thew Feb 12 '15 at 00:29
  • try `manage.py syncdb`. – levi Feb 12 '15 at 00:41
  • I tried syncdb again. No change in the behavior. – Thew Feb 12 '15 at 01:19

3 Answers3

0

The urlpatterns looks wrong in core/urls.py instead of doing

urlpatterns = patterns('',
url(r'^', views.home, name='home'),)

try

   urlpatterns = [url(r'^','core.views.home',name='home'),]

P.S. The views.name is quoted

cmidi
  • 1,880
  • 3
  • 20
  • 35
  • This is one of the combinations I've tried. I added the `from core import views` in order to be able to use it in the way it is now. To be thorough though, I changed it as you described and am still seeing the same issue. – Thew Feb 12 '15 at 02:18
  • @Thew I am sorry 'core.views.home', without import, sometimes import causes circular dependency – cmidi Feb 12 '15 at 02:20
  • I removed the import declaration and added the quoted `core.views.home`. I tried both the `urlpatterns = patterns(...)` and the `urlpatterns = [...]`, but to no avail. I'm still getting the "No module named urls" at all paths. – Thew Feb 12 '15 at 02:32
  • How does the the get request looks like ? any reason to import admin in your core/urls.py file ? – cmidi Feb 12 '15 at 03:00
  • I've updated the core/urls.py and I've added the contents of core/views.py. – Thew Feb 12 '15 at 18:21
  • For just a sanity test can you make sure that there are no stale .pyc files in your project directory, can you remove the urls.pyc and settings.pyc from your project and test again. Currently there is nothing that looks to be a problem. expect that your trace does not look similar to your mysite/urls.py code any idea why this is in your trace ? `url(r'^aa/', include('core.urls', namespace="aa")),` Also can you specify a name of the app in your mysite/urls.py something on the lines of `url(r'^polls/', include('core.urls', namespace='core', app_name='core')),` – cmidi Feb 12 '15 at 19:37
  • The aa is what's actually in the urls.py files. I've been replacing it with 'core' for the sake of clarity in this question. I've been extremely thorough to make sure that I'm being consistent though. As for the pyc files, I delete them all every time I make a change to be certain that I'm testing updated logic. – Thew Feb 12 '15 at 19:40
  • Sorry my previous comment had wrong url name url(r'^core/', include('core.urls', namespace='core', app_name='core')), – cmidi Feb 12 '15 at 19:47
  • I tried adding the app_name to the url declaration. This offered no change. 200 from shell, 500 from browser. – Thew Feb 12 '15 at 20:00
  • The only think I can now suggest is to enable the DEBUG on settings and check the local variables when in it trying to import by setting in settings.py `DEBUG = True TEMPLATE_DEBUG = True` this provides the trace with local variables in the browser something like `No module named urls import_modul __import__(name) Local vars Variable Value name results.urls' Package None ` The only way I can reproduce your problem is when I do not have a urls.py in my app. – cmidi Feb 12 '15 at 20:25
  • DEBUG & TEMPLATE_DEBUG have both been set to True for the duration of my troubleshooting and also for the stack trace I posted. I know that django is finding both url files as the shell testing functions as intended. It's only failing to find the core.urls when called remotely via browser. Hopefully, DreamHost will have some suggestions as to why that might be. – Thew Feb 12 '15 at 20:36
0

Just a hunch, but try swapping out url(r'^core/', include('core.urls', namespace="core")) with (r'^core/', include('core.urls')) in mysite/urls.py.

Sean Azlin
  • 886
  • 7
  • 21
  • I tried all combinations of removing the namespace and the url prefix. Both left me with a 200 response from `django.test.Client` but a 500 from the browser. – Thew Feb 12 '15 at 18:30
  • Can you post more details about the 500 message? – Sean Azlin Feb 12 '15 at 18:56
  • There's a link to the stack trace in the question (right before the first bolded "Edit"). The exception type is "ImportError" and the exception value is "No module named urls". – Thew Feb 12 '15 at 19:00
  • Are the permissions set properly for the core directory? – Sean Azlin Feb 12 '15 at 19:04
  • I double checked to be sure. The permissions for the core directory and it's contents mirror that of polls and mysite. (which function perfectly if I remove any calls to core from mysite/urls.py) – Thew Feb 12 '15 at 19:07
0

I finally found a resolution.

I tried creating a completely new project and adding the 'core' app, but was seeing the exact same issue. It dawned on me that the reason that it wasn't able to access submodules (views, models, urls) is that the 'core' module that was being loaded by passenger wasn't the 'core' module that I had created.

I tried changing the name of the app and the issue was resolved.

I noticed that when trying to create an app named 'test' Django throws you a message indicating that it isn't allowed due to an existing module. Apparently this isn't the case across the board.

To follow up, I reviewed my passenger_wsgi.py file (also in the question):

import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/mysite')

if sys.version < "2.7.9":
   os.execl(cwd + "/env/bin/python", "python2.7", *sys.argv)

sys.path.insert(0, cwd + '/env/bin')
sys.path.insert(0, cwd + '/env/lib/python2.7')
sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages/django')
sys.path.insert(0, cwd + '/env/lib/python2.7/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

After looking back, I see the error. In following DreamHost's setup guide for alternative versions of python (found here), I had inserted my env's python path to the beginning of the sys.path. This causes the django.core module to be matched before my project app which was appended to the end of sys.path.

Moral of the story is be aware of module names and path construction.

Thew
  • 61
  • 9