ModuleNotFoundError - but it exists.

Hi. I have just started building my first django app and am currently stuck at the part where I’m supposed to be executing a makemigrations command to create a new model called ‘userprofile’ right after I have added a reference to the configuration class in the INSTALLED_APPS setting.

Upon executing the ‘python manage.py makemigrations’ command in the terminal, I received this error:

Traceback (most recent call last):
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\config.py", line 244, in create
    app_module = import_module(app_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'core'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\config.py", line 250, in create
    app_config_class.__qualname__,
django.core.exceptions.ImproperlyConfigured: Cannot import 'core'. Check that 'apps.core.apps.CoreConfig.name' is correct.

This is my settings.py file. I have added the references “apps.core” and “apps.userprofile”:

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

    'apps.core',
    'apps.userprofile',
]

When I removed ‘apps.core’, from INSTALLED_APPS and executed the ‘python manage.py makemigrations’ command in the terminal, I received this error:

Traceback (most recent call last):
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\config.py", line 244, in create
    app_module = import_module(app_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'userprofile'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\apps\config.py", line 250, in create
    app_config_class.__qualname__,
django.core.exceptions.ImproperlyConfigured: Cannot import 'userprofile'. Check that 'apps.userprofile.apps.UserprofileConfig.name' is correct.

When I execute the command ‘python manage.py makemigrations userprofile’ in the terminal, I receive the same error displayed above.

My ‘userprofile’ directory listings are as follows:

userprofile
userprofile_pycache_
userprofile\migrations
userprofile\migrations_init_.py
userprofile_init_.py
userprofile\admin.py
userprofile\apps.py
userprofile\models.py
userprofile\tests.py
userprofile\views.py

(The userprofile folder [and another folder called core] is in a folder called apps; which is in a folder called olasac).

This is my apps.py file:

from django.apps import AppConfig


class UserprofileConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'userprofile'

This is my models.py file:

#
#

from django.contrib.auth.models import User
from django.db import models


#
# Models

class Userprofile(models.Model):
    user = models.ForeignKey(User, related_name='userprofile', on_delete=models.CASCADE)
    active_team_id = models.IntegerField(default=0)

Django version-3.2.14
Python version-3.10.5
Pip version-20.1.1

I have also included a screenshot of my settings.py file in case there is some info I forgot to mention. The entire directory is visible in the image. Would have liked to add more but new users can only add one item of embedded media.

I suspect the problem may be due to the placement of my file directories but at this point, I’ve hit a brick wall and am not able to proceed with the project. Any advice?

Thanks in advance!

Unfortunately, that image is unreadable. Please do not post images here of code or other text data - copy/paste the text as needed in your posts.

It looks to me from your description that your structure looks something like this:

olasac/  - The project base directory
    apps/
        userprofile/
        core/

Is this an accurate description of your directory structure?

What directory contains your settings.py file? What’s your current directory when you’re running manage.py?

Note: If this is an accurate description, this is not the “standard” / “typical” Django project organization. Typically, you would have the userprofile and core directories directly under the olasac directory.

Yes, this directory structure is accurate.

The settings.py file is in another folder called olasac.

olasac/    - The project base directory (the manage.py file is in this folder).
      apps/
              core/
              userprofile/
      olasac/
             settings.py

manage.py:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'olasac.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

There are two things I can think of for you to try:

  • Move your core and userprofile directories up one level and remove the reference to apps from the references to those packages.

  • Add an __init__.py file in each of the apps, core, and userprofile directories.

Note: If you keep this directory structure, then all your “cross-app” module references will need to specify that apps prefix.

I tried your second solution first; to add an __init__.py file in each of the apps , core , and userprofile directories. This made no difference.

I then moved the core and userprofile directories up one level and removed the reference to apps from the references to those packages:

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

    'core',
    'userprofile',
]

I recieved this error:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\base.py", line 393, in execute
    self.check()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\management\base.py", line 423, in check
    databases=databases,
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\urls\resolvers.py", line 416, in check
    for pattern in self.url_patterns:
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\urls\resolvers.py", line 602, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Hillis\Documents\Olasac\olasac_3_10_5\lib\site-packages\django\urls\resolvers.py", line 595, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Hillis\Documents\Olasac\olasac\olasac\urls.py", line 19, in <module>
    from apps.core.views import frontpage, privacy, terms, plans
ModuleNotFoundError: No module named 'apps.core'
1 Like

By moving those files up one level, you need to remove all references to apps.

Thanks very much! Working well now.
All the best.