First of all: I am well aware that there are several questions with this exact error but I've read at least 20 of them and none helped me correct this error.
This being said, the problem is the following:
I created a directory called 'api' inside my Django project to store all the code related to the api. Inside this directory I created an app called 'profiles_app'. There, in the models.py file I defined a UserProfile class to override the default Django UserProfile class. I then added the app to the INSTALLED_APPS variable in the settings.py file and overrid the default UserProfile class with the following line:
AUTH_USER_MODEL = 'profiles_api.UserProfile'
Finally I tried to make the migrations to see the new table in the database and this is when the error occurred.
This is the stack trace:
(venv) C:\Users\Juan Pablo\Documents\backend\backend_django>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\JUANPA~1\Envs\venv\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\JUANPA~1\Envs\venv\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 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'
As you can see in the trace I am working in a virtual environment and this is what I have installed:
asgiref==3.2.3
Django==3.0.3
djangorestframework==3.11.0
mysqlclient==1.4.6
pytz==2019.3
sqlparse==0.3.0
This is the project's structure:
The init.py file inside the api directory wasn't there at first but after reading some answers I created a Python package instead of a directory and then that file appeared.
Finally, this is what the INSTALLED_APPS variable in the settings.py file looks like:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'api.profiles_api',
]
I have tried the following:
'profiles_api' instead of 'api.profiles_api' in INSTALLED_APPS
AUTH_USER_MODEL = 'api.profiles_api.UserProfile' instead of 'profiles_api.UserProfile'
Creating a Python package named 'api' instead of a directory.
Any help is really appreciated. Sorry for the long question but I wanted to be as precise as possible.