My Django dev environment runs fine with the native server, but when I try to execute wsgi.py
, either locally or on the production server, I get the extremely popular ModuleNotFoundError: No module named 'config'
error.
The traceback suggests that the failure happens when django/core/wsgi.py
attempts to execute django.setup(set_prefix=False)
and that method needs to call settings.[ANYTHING]
, in this case configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
.
Calling the settings.LOGGING_CONFIG
variable tells django.conf.settings
to initialize, but in this context it fails:
Traceback (most recent call last):
File "./config/wsgi.py", line 35, in <module>
application = get_wsgi_application()
File "C:\...\anaconda\envs\venvname\lib\site-packages\django\core\wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "C:\...\anaconda\envs\venvname\lib\site-packages\django\__init__.py", line 18, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\...\anaconda\envs\venvname\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
File "C:\...\anaconda\envs\venvname\lib\site-packages\django\conf\__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "C:\...\anaconda\envs\venvname\lib\site-packages\django\conf\__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\...\anaconda\envs\venvname\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'config'
The relevant pieces of my directory structure look like this:
parent-folder/
config/
__init.py__
wsgi.py
settings/
__init.py__
local.py
production.py
projectname/
My wsgi.py
file looks like this:
import os
import sys
from pathlib import Path
from django.core.wsgi import get_wsgi_application
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent
sys.path.append(str(ROOT_DIR / "projectname"))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
application = get_wsgi_application()
I suspect this is something that I'll fix with a one-line change to a path or module reference somewhere, but nothing has worked so far. Any help is greatly appreciated.
EDIT:
The traceback shows that when django/conf/__init__.py
executes LazySettings.setup()
, the settings_module
is successfully extracted from os.environ.get(ENVIRONMENT_VARIABLE)
. So the problem is downstream of that, FWIW.
CodePudding user response:
In wsgi.py file, settings module is "config.settings.production", this means that settings directory should be a module. Therefore add an empty
__init__.py
file into the settings directory to solve the problem.
CodePudding user response:
Razenstein's comment is the correct answer: "you just need to add the parent-folder to sys.path the same way as you do for folder projectname." My path was missing the enclosing folder.