I have a Django deployment with Apache24 that was working as expected previously, with the django project name being "API_Materials". However, when I tried to make new migrations with python manage.py makemigrations
, a command that never had any problem before, I got the following stacktrace:
(venv) C:\API\BD_API_Materials>python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\base.py", line 453, in execute
self.check()
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\base.py", line 485, in check
all_issues = checks.run_checks(
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\checks\urls.py", line 36, in check_url_namespaces_unique
if not getattr(settings, "ROOT_URLCONF", None):
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 102, in __getattr__
self._setup(name)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 89, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 217, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'MaterialsAPI'
The module that the project tried to use is called "MaterialsAPI", while the project is "API_Materials". However, "MaterialsAPI" is likely a custom name created for this project, however this text doesn't exist in any existent file in the project.
First I tried going "back on time" with git reset --hard
with an external backup of the current situation, but this didn't fix the problem. While I was attempting to verify if this was specific for makemigrations
command, I tried a non-existent command makemigration
to check, but the error still persisted:
(venv) C:\API\BD_API_Materials>python manage.py makemigration
Traceback (most recent call last):
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\__init__.py", line 255, in fetch_command
app_name = commands[subcommand]
KeyError: 'makemigration'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\API\BD_API_Materials\manage.py", line 22, in <module>
main()
File "C:\API\BD_API_Materials\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\core\management\__init__.py", line 262, in fetch_command
settings.INSTALLED_APPS
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 102, in __getattr__
self._setup(name)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 89, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\afonso.campos\Desktop\BD_API_Materials\API\venv\lib\site-packages\django\conf\__init__.py", line 217, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'MaterialsAPI'
Analyzing the stacktrace, it looked like the error was being triggered because he exists in the INSTALLED_APPS
section in settings.py
, but that was not the case.
INSTALLED_APPS = [
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'API_Materials.apps.ApiMaterialsConfig',
'rest_framework',
'rest_framework.authtoken',
'django_extensions',
'drf_yasg',
'cryptographic_fields'
]
With more debugging, I noticed that the directory changed in the middle of the execution (before being " C:\API\BD_API_Materials\ ", the correct directory for the project, and later going to a specific user. This happened maybe because of the apache setup, which used the wrong virtual environment directory, but I attempted to change it and restart the Apache server, which still wouldn't work. In this wrong directory, however, the word "MaterialsAPI" still didn't exist neither in this environment.
Observing the environment variables before the line which cause the error settings.INSTALLED_APPS
, the text "MaterialsAPI" was found, with the wrong directory:
environ({
...
'VIRTUAL_ENV': 'C:\\Users\\afonso.campos\\Desktop\\BD_API_Materials\\API\\venv',
'DJANGO_SETTINGS_MODULE': 'MaterialsAPI.settings'
...
})
Then, I proceed to attempt to "force" the changes in the virtual environment, however when I run the manage.py
command, they are reseted to their "wrong" values.
Lastly, this error doesn't occur when I test the project locally in a different environment.
Well, this is embarassing... Aparently the manage.py
django file had the line os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MaterialsAPI.settings')
, but not locally, only on git and in deployment. This for some reason passed by the verification in Visual Studio Code to verify "MaterialsAPI" in any file inside the project. I don't know when this change happened, since the local project never complained about it.
Changing to os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'API_Materials.settings')
solved the problem by returning to the original project's name "API_Materials".