Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not import/No module named Django Error with Apache

Tags:

apache

django

I had a small proof of concept set up on a development server on a local machine. I'm now trying to move it over to django on a production server, which I'm using webfaction for. However, now that I'm switched over to apache from the built in django server I get the following:

ViewDoesNotExist: Could not import orgDisplay.views. Error was: No module named orgDisplay.views

But when check my orgDisplay apps folder there is a view.py in it. What am I doing wrong? I've tried adding the following to my settings.py by suggestion of the django IRC room.

import sys
sys.path.append(r"/home/user/webapps/django_project/myproject/orgDisplay")

which is the path to my app.

any ideas on how to even begin to trouble shoot this?

Thanks in advance.

like image 214
Noah Clark Avatar asked Nov 21 '25 02:11

Noah Clark


2 Answers

I assume you're using mod_wsgi (which is recommended by Django authors), not mod_python. This is the way you should use your sys.path:

django.wsgi:

import os, sys
sys.path.append(r"/home/user/webapps/django_project/myproject/")
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

sys.stdout = sys.stderr # Prevent crashes upon print

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

urls.py:

from django.conf.urls.defaults import *
urlpatterns = (
   ("", include("orgDisplay.urls")),
   # ...
)

orgDisplay/urls.py:

import views

urlpatterns = ( 
    (r'^some_view/$', views.some_view), # It is actually orgDisplay.views.some_view
    # many more records ...
)

It is a bad idea to add project dir itself to path since you're be getting name conflicts between multiple projects.

like image 117
Alexander Lebedev Avatar answered Nov 22 '25 17:11

Alexander Lebedev


I think you're appending the wrong directory to sys.path. I think Python is looking in the .../myproject/orgDisplay folder for the orgDisplay package. Try removing the orgDisplay from your string, like this:

import sys
sys.path.append(r"/home/user/webapps/django_project/myproject")

The other option would be to simply add myproject (or whatever your project is actually called) in the import statement.

# instead of "from orgDisplay import views"
from myproject.orgDisplay import views

Also, make sure to restart Apache after every edit.

like image 42
Cassie Meharry Avatar answered Nov 22 '25 17:11

Cassie Meharry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!