'Django - WSGI script cannot be loaded as Python module

This is a well-known issue it seems but I have spent 8 hours trying to fix the issue without any success.

I have been running a Django project with Apache2 on this VPS before, this issue occurred when I deployed new code and ran a sudo apt-get update. I now receive an Internal server error when trying to reach the website.

I am desperate, does anyone have a suggestion of what could have happend?

What I have installed on my Ubuntu 18.04 VPS

  - sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3 
  - sudo apt-get install mysql-server  
  - sudo mysql_secure_installation 
  - sudo apt install git  
  - pip3 install virtualenv  
  - pip3 install django 
  - sudo apt-get install libmysqlclient-dev  
  - pip3 install mysqlclient

Error log

[Fri Mar 06 22:47:29.96579 2020] mod_wsgi (pid=4877): Target WSGI script '/var/www/project/project/wsgi.py' cannot be loaded as Python module.
[Fri Mar 06 22:47:29.965710 2020] mod_wsgi (pid=4877): Exception occurred processing WSGI script '/var/www/project/project/wsgi.py'.
[Fri Mar 06 22:47:29.965712 2020] mod_wsgi (pid=16955): Target WSGI script '/var/www/project/project/wsgi.py' cannot be loaded as Python module.
[Fri Mar 06 22:47:29.965917 2020] mod_wsgi (pid=16955): Exception occurred processing WSGI script '/var/www/project/project/wsgi.py'.
[Fri Mar 06 22:47:29.967013 2020] Traceback (most recent call last):
[Fri Mar 06 22:47:29.967585 2020] File "/var/www/project/project/wsgi.py", line 17, in <module>
[Fri Mar 06 22:47:29.967628 2020] application = get_wsgi_application()
[Fri Mar 06 22:47:29.967703 2020] File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Mar 06 22:47:29.967737 2020] django.setup(set_prefix=False)
[Fri Mar 06 22:47:29.967815 2020] File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 19, in setup
[Fri Mar 06 22:47:29.967854 2020] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Fri Mar 06 22:47:29.967934 2020] File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 76, in __getattr__
[Fri Mar 06 22:47:29.967969 2020] self._setup(name)
[Fri Mar 06 22:47:29.968036 2020] File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 63, in _setup
[Fri Mar 06 22:47:29.968068 2020] self._wrapped = Settings(settings_module)
[Fri Mar 06 22:47:29.968139 2020] File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 142, in __init__
[Fri Mar 06 22:47:29.968168 2020] mod = importlib.import_module(self.SETTINGS_MODULE)
[Fri Mar 06 22:47:29.968238 2020] File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
[Fri Mar 06 22:47:29.968273 2020] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Mar 06 22:47:29.968347 2020] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Fri Mar 06 22:47:29.968419 2020] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Fri Mar 06 22:47:29.968488 2020] File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
[Fri Mar 06 22:47:29.968553 2020] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Fri Mar 06 22:47:29.968662 2020] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Fri Mar 06 22:47:29.968734 2020] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Fri Mar 06 22:47:29.968809 2020] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Mar 06 22:47:29.968934 2020] ModuleNotFoundError: No module named 'project'

My 000-default.conf (Apache2)

 <VirtualHost *:80>
    ServerName MY IP HERE
    DocumentRoot /var/www/project

    WSGIDaemonProcess MY IP HERE python-path=/usr/lib/python3.6
    #WSGIDaemonProcess MY IP HERE python-home=/var/www/project python-path=/usr/lib/python3.6 
    WSGIProcessGroup MY IP HERE
    WSGIScriptAlias / /var/www/project/project/wsgi.py

    <Directory /var/www/project/myapp>
            AllowOverride all
            Require all granted
            Options FollowSymlinks
    <Files wsgi.py>
    Require all granted
    </Files>
    </Directory>

    Alias /static/ /var/www/project/myapp/static/

    <Directory /var/www/project/myapp/static>
            Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

My settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'myKey'

DEBUG = True

ALLOWED_HOSTS = ['myIP']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'django.contrib.humanize',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'project.wsgi.application'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': 'pass',
        'HOST': '',
        'PORT': '',
       # 'OPTIONS': {
       # 'read_default_file': '/etc/mysql/my.cnf',
       # }
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'CET'

USE_I18N = True

USE_L10N = True

USE_TZ = False

USE_THOUSAND_SEPARATOR = True

STATIC_ROOT = '/static/'
STATIC_URL = '/static/'
LOGIN_URL = 'index'


Solution 1:[1]

Add var/www/project to your PYTHONPATH.

Just add the following lines to wsgi.py:

import sys

sys.path.append('/var/www/project/')

as per OP @igteraous' own edit to the question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vinzee