2

I'm new to Django and this is a first time I'm trying to deploy my Django application on WebFaction. I searched on StackOverflow but did not find someone who encountered the same problem.

My app was developed using Django 2.2 and Python 3.7.3. I have run successfully makemigrations, migrate, collectstatic on my computer's localhost and run successfully those commands when using PuTTY on Windows getting SSH to WebFaction server.

However when I restart the WebFaction server and access using browser, it returned ModuleNotFoundError.

ModuleNotFoundError at /

No module named 'app_www_admission.urls'

Request Method:     GET
Request URL:    (my website's URL)
Django Version:     2.1.7
Exception Type:     ModuleNotFoundError
Exception Value:    

No module named 'app_www_admission.urls'

Exception Location:     <frozen importlib._bootstrap> in _find_and_load_unlocked, line 965
Python Executable:  /usr/local/bin/python3
Python Version:     3.7.3
Python Path:    

['/home/marknguyen94/webapps/adm_220_2',
 '/home/marknguyen94/webapps/adm_220_2/app_www_admission',
 '/home/marknguyen94/webapps/adm_220_2/lib/python3.7',
 '/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg',
 '/home/marknguyen94/webapps/adm_220_2/lib/python3.7/pytz-2019.1-py3.7.egg',
 '/home/marknguyen94/lib/python3.7',
 '/usr/local/lib/python37.zip',
 '/usr/local/lib/python3.7',
 '/usr/local/lib/python3.7/lib-dynload',
 '/home/marknguyen94/.local/lib/python3.7/site-packages',
 '/usr/local/lib/python3.7/site-packages']

Server time:    Wed, 24 Apr 2019 10:48:37 +0000

I tried resolving the package that I imported in these files: admin.py, forms.py, models.py, settings.py, main urls.py, the app's urls.py, views.py and I realized that:

  1. For development, I must import using:
    from app_www_admission.models import ...

  2. For production, when I restart the Webfaction server, I must import using:
    from app_www_admission.app_www_admission.models import ...

The option (1) lets me run the makemigrations, migrate, collectstatic, check, etc on commands but resulted ModuleNotFoundError when visiting the website by browser as I mentioned above. The error log by the server is attached below.
The option (2) lets me visit the website by browser as I expected (no more ModuleNotFoundError in the browser) but I can't do anything with commands makemigrations, migrate, collectstatic, check, etc because it always throws
ModuleNotFoundError: No module named 'app_www_admission.app_www_admission'.

For more information, I deployed by copying all files from local computer to the WebFaction, installed pip, and neccessary packages but it seems that the problem is with the app_www_admission. Now what I can do is: switch to (1), run all my necessary commands (to migrate or something), then switch to (2) by editting all related files on server, then restart server. Switching between these options is very stressful and I knew I must be silly and wrong somewhere.

Could you please tell me what was wrong with the import paths that I mentioned or what was wrong with the settings or something? I really appreciate all your help.

My project structure on WebFaction:

/
├── home
│   ├── marknguyen94
│   |   ├── .cache
│   |   ├── .local
│   |   ├── .pki
│   |   ├── .subversion
│   |   ├── bin
│   |   ├── certificates
│   |   ├── lib
│   |   ├── logs
│   |   ├── webapps
│   |   |   ├── adm_static
│   |   |   ├── adm_220_2
│   |   |   |   ├── apache2
│   |   |   |   ├── bin
│   |   |   |   ├── lib
│   |   |   |   ├── app_www_admission
│   |   |   |   |   ├── __init__.py
│   |   |   |   |   ├── manage.py
│   |   |   |   |   ├── conf
│   |   |   |   |   ├── locale
│   |   |   |   |   ├── app_www_admission
│   |   |   |   |   |   ├── __init__.py
│   |   |   |   |   |   ├── admin.py
│   |   |   |   |   |   ├── forms.py
│   |   |   |   |   |   ├── models.py
│   |   |   |   |   |   ├── settings.py
│   |   |   |   |   |   ├── tokens.py
│   |   |   |   |   |   ├── urls.py
│   |   |   |   |   |   ├── wsgi.py
│   |   |   |   |   |   ├── db.sqlite3
│   |   |   |   |   |   ├── migrations
│   |   |   |   |   |   ├── static
│   |   |   |   |   |   ├── templates
│   |   |   |   |   |   ├── tekkyhouse
│   |   |   |   |   |   |   ├── __init__.py
│   |   |   |   |   |   |   ├── admission
│   |   |   |   |   |   |   |   ├── __init__.py
│   |   |   |   |   |   |   |   ├── django
│   |   |   |   |   |   |   |   |   ├── __init__.py
│   |   |   |   |   |   |   |   |   ├── home
│   |   |   |   |   |   |   |   |   |   ├── __init__.py
│   |   |   |   |   |   |   |   |   |   ├── urls.py
│   |   |   |   |   |   |   |   |   |   ├── views.py

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_www_admission.app_www_admission.settings')

application = get_wsgi_application()

some sections in settings.py

BASE_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "app_www_admission")
print ("BASE_DIR: ", BASE_DIR)

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'app_www_admission',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.linkedin',
    'allauth.socialaccount.providers.linkedin_oauth2',
]

# this works for development server
ROOT_URLCONF = 'app_www_admission.urls'

# this works for production
# ROOT_URLCONF = 'app_www_admission.app_www_admission.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR + "/templates", ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
        },
    },
]

WSGI_APPLICATION = 'app_www_admission.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LOCALE_PATHS = [
  os.path.join(BASE_DIR, 'locale'),
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
    ('vi', _('Vietnamese')),
]

STATIC_ROOT = '/home/marknguyen94/webapps/adm_static/'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
print("STATICFILES_DIRS: ", STATICFILES_DIRS)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)
print("TEMPLATES_DIRS: ", TEMPLATE_DIRS)


TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    # 'django.template.loaders.eggs.Loader',
)

The "import section" of admin.py, forms.py,

# this works for development server
from app_www_admission.models import UserProfile

# this works for production
# from app_www_admission.app_www_admission.models import UserProfile

The "import section" of views.py

from django.shortcuts import render
from django.views import View
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from django.db.models import Max
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.core.mail import EmailMessage
import json

# these work for development server
from app_www_admission.models import UserProfile, GameSession
from app_www_admission.forms import UserModelForm
from app_www_admission.tokens import account_activation_token

# these work for production
# from app_www_admission.app_www_admission.models import UserProfile, GameSession
# from app_www_admission.app_www_admission.forms import UserModelForm
# from app_www_admission.app_www_admission.tokens import account_activation_token

The main urls.py

from django.urls import include
from django.urls import path
from django.views.i18n import JavaScriptCatalog
from django.contrib import admin
# from django.contrib.staticfiles.urls import staticfiles_urlpatterns

js_info_dict = {
    'domain': 'djangojs',
    'packages': ('app_www_admission',),
}

urlpatterns = [
    path('admin/', admin.site.urls),

    # this works for development server
    path('', include('app_www_admission.tekkyhouse.admission.django.home.urls')),

    # this works for production
    # path('', include('app_www_admission.app_www_admission.tekkyhouse.admission.django.home.urls')),

    # # Localization
    path('i18n/', include('django.conf.urls.i18n')),
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]

# urlpatterns += staticfiles_urlpatterns()

The app's urls.py

from django.urls import path, include
from django.conf.urls import url

# this works for development server
from app_www_admission.tekkyhouse.admission.django.home import views

# this works for production
# from app_www_admission.app_www_admission.tekkyhouse.admission.django.home import views


urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),
    path('activate/<str:uid>/<str:token>', views.Activate.as_view(), name='activate'),
    path('register', views.RegisterView.as_view(), name='register'),
    path('game', views.GameView.as_view(), name='game'),
    path('gameover', views.GameOverView.as_view(), name='gameover'),
    path('gamefinished', views.GameFinishedView.as_view(), name='gamefinished'),
    url(r'^accounts/', include('allauth.urls')),
]

WebFaction server's error log for option (1)

[Wed Apr 24 10:55:50.407745 2019] [mpm_worker:notice] [pid 15709:tid 140385667327872] AH00292: Apache/2.4.37 (Unix) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Wed Apr 24 10:55:50.407853 2019] [core:notice] [pid 15709:tid 140385667327872] AH00094: Command line: '/home/marknguyen94/webapps/adm_220_2/apache2/bin/httpd.worker -f /home/marknguyen94/webapps/adm_220_2/apache2/conf/httpd.conf'
[Wed Apr 24 10:56:00.948624 2019] [wsgi:error] [pid 31535:tid 140385586988800] BASE_DIR:  /home/marknguyen94/webapps/adm_220_2/app_www_admission/app_www_admission
[Wed Apr 24 10:56:00.948979 2019] [wsgi:error] [pid 31535:tid 140385586988800] STATICFILES_DIRS:  ('/home/marknguyen94/webapps/adm_220_2/app_www_admission/app_www_admission/static',)
[Wed Apr 24 10:56:00.949025 2019] [wsgi:error] [pid 31535:tid 140385586988800] TEMPLATES_DIRS:  ('/home/marknguyen94/webapps/adm_220_2/app_www_admission/app_www_admission/templates',)
[Wed Apr 24 10:56:01.273233 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672] Internal Server Error: /
[Wed Apr 24 10:56:01.273311 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672] Traceback (most recent call last):
[Wed Apr 24 10:56:01.273337 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/core/handlers/exception.py", line 34, in inner
[Wed Apr 24 10:56:01.273361 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     response = get_response(request)
[Wed Apr 24 10:56:01.273384 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/core/handlers/base.py", line 111, in _get_response
[Wed Apr 24 10:56:01.273408 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     resolver_match = resolver.resolve(request.path_info)
[Wed Apr 24 10:56:01.273429 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/urls/resolvers.py", line 491, in resolve
[Wed Apr 24 10:56:01.273452 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     for pattern in self.url_patterns:
[Wed Apr 24 10:56:01.273473 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/utils/functional.py", line 37, in __get__
[Wed Apr 24 10:56:01.273496 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     res = instance.__dict__[self.name] = self.func(instance)
[Wed Apr 24 10:56:01.273518 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/urls/resolvers.py", line 533, in url_patterns
[Wed Apr 24 10:56:01.273541 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
[Wed Apr 24 10:56:01.273564 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/utils/functional.py", line 37, in __get__
[Wed Apr 24 10:56:01.273648 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     res = instance.__dict__[self.name] = self.func(instance)
[Wed Apr 24 10:56:01.273676 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/home/marknguyen94/webapps/adm_220_2/lib/python3.7/Django-2.1.7-py3.7.egg/django/urls/resolvers.py", line 526, in urlconf_module
[Wed Apr 24 10:56:01.273700 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     return import_module(self.urlconf_name)
[Wed Apr 24 10:56:01.273721 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Wed Apr 24 10:56:01.273766 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]     return _bootstrap._gcd_import(name[level:], package, level)
[Wed Apr 24 10:56:01.273791 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Wed Apr 24 10:56:01.273814 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Wed Apr 24 10:56:01.273837 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672]   File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
[Wed Apr 24 10:56:01.273860 2019] [wsgi:error] [pid 31535:tid 140385586988800] [remote 127.0.0.1:57672] ModuleNotFoundError: No module named 'app_www_admission.urls'
Mark Nguyen
  • 91
  • 2
  • 6
  • Looks like your `sys.path` is off by one directory. python looks in `/home/marknguyen94/webapps/adm_220_2/app_www_admission` and not in `/home/marknguyen94/webapps/adm_220_2/app_www_admission/app_www_admission` for modules to import; hence you having to prepend another `app_www_admission` to your imports. Have you tried setting up a virtual environment and running your app from within that? Try `python manage.py shell --pythonpath=/home/marknguyen94/webapps/adm_220_2/app_www_admission/app_www_admission` on your server. – CoffeeBasedLifeform Apr 25 '19 at 20:13
  • Check this out: [How to use Django with Apache and mod_wsgi](https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi). Make sure `WSGIPythonPath` points to the right directory. – CoffeeBasedLifeform Apr 26 '19 at 05:50
  • I have some django apps still with Webfaction, the guide is very good https://docs.webfaction.com/software/django/getting-started.html, but you must've read it cause I see you have the proper setup in `webapps` (static, and django app) – ionescu77 May 03 '19 at 12:21
  • You must've solved it by now, I guess you had a look in `/home/marknguyen94/webapps/adm_220_2/apache2/conf/httpd.conf` and checked the WSGI* variables in that apache config like someone here sugested. – ionescu77 May 03 '19 at 12:30
  • @CoffeeBasedLifeform Many thanks to you since I followed your reply and everything worked as I expected. And thank you all people answered, I also took note your idea for further cases. – Mark Nguyen Jun 21 '19 at 03:40

0 Answers0