Search code examples
pythondjangoservermod-wsgimodulenotfounderror

I am getting 500 Internal Server Error in django app during deloying on production server ModuleNotFoundError: No module named 'main' (wsgi:error)


I want to serve my project using mod_wsgi. When i run development server, It's started but it gives error on production server.My os is RedHat and using httpd(apache) virtual host configured with python3-mod_wsgi.

This is my error

[Tue Oct 05 14:01:48.998236 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776] mod_wsgi (pid=423542): Failed to exec Python script file '/var/www/public_html/main/main/wsgi.py'.
[Tue Oct 05 14:01:48.998291 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776] mod_wsgi (pid=423542): Exception occurred processing WSGI script '/var/www/public_html/main/main/wsgi.py'.
[Tue Oct 05 14:01:48.998461 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776] Traceback (most recent call last):
[Tue Oct 05 14:01:48.998506 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/var/www/public_html/main/main/wsgi.py", line 17, in <module>
[Tue Oct 05 14:01:48.998523 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     application = get_wsgi_application()
[Tue Oct 05 14:01:48.998530 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Oct 05 14:01:48.998534 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     django.setup(set_prefix=False)
[Tue Oct 05 14:01:48.998540 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
[Tue Oct 05 14:01:48.998543 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Oct 05 14:01:48.998548 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
[Tue Oct 05 14:01:48.998552 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     self._setup(name)
[Tue Oct 05 14:01:48.998557 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
[Tue Oct 05 14:01:48.998560 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     self._wrapped = Settings(settings_module)
[Tue Oct 05 14:01:48.998565 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
[Tue Oct 05 14:01:48.998569 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Oct 05 14:01:48.998574 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
[Tue Oct 05 14:01:48.998577 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Oct 05 14:01:48.998582 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Tue Oct 05 14:01:48.998587 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Tue Oct 05 14:01:48.998592 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
[Tue Oct 05 14:01:48.998597 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Tue Oct 05 14:01:48.998602 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Tue Oct 05 14:01:48.998614 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Tue Oct 05 14:01:48.998619 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776]   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Tue Oct 05 14:01:48.998633 2021] [wsgi:error] [pid 423542:tid 140143400638208] [remote 209.141.62.185:36776] ModuleNotFoundError: No module named 'main'

This is my project wsgi.py file

import os
import sys
#from django.conf import settings
from django.core.wsgi import get_wsgi_application
#sys.path.append('/var/www/public_html/main/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

application = get_wsgi_application()


Solution

  • I Changed my wsgi.py file to this now it's working

    """
    WSGI config for main project.
    
    It exposes the WSGI callable as a module-level variable named ``application``.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
    """
    
    import os
    import sys
    
    from django.core.wsgi import get_wsgi_application
    sys.path.append('/var/www/public_html/')
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
    
    application = get_wsgi_application()