Search code examples
pythondjangodjango-urlsboilerplate

Django NoModuleFoundError occurs only when adding a valid path to urls.py of my project folder


I have set up a Django project according to Victor Freitas' excellent post on production ready Django boilerplate here https://simpleisbetterthancomplex.com/tutorial/2021/06/27/how-to-start-a-production-ready-django-project.html

It took me a day to refactor my whole project with 7 apps to fit into that boilerplate. All was fine and with everything working until I started developing my urls paths and templates.

For some reason when adding a url path to main urls.py file within the main project folder Django fires me a NoModuleFoundError stating 'ModuleNotFoundError: No module named 'categories' . Categories is the name of my app and it is properly installed in base.py config file. Code below:

# SIGP3A/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # path('categories/', include('categories.urls')), # <-- THIS LINE RIGHT HERE
]

If I uncomment the above pointed line I get the error message. If I comment it out it passes Django checks.

See bellow bits and pieces of the code that I believe are relevant to the question:

code structure up to setting local

# SIGP3A/Comps/categories/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.categories, name='categories'),
]

See below my installed apps where categories is included. Note Comps is INSIDE main project folder and it is where I keep all my apps.

base.py config file showing installed apps

I am using local.py config which imports from base.py. Like I said, everything works BEFORE I start adding url paths.

I commented out everything related to django debug toolbar just to filter out a few things too while debuging it.

See below how categories is registered in apps.py:

from django.apps import AppConfig


class CategoriesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'SIGP3A.Comps.categories'

I spent my morning investigating this with trials and errors only to find differing errors that led me even farther from a solution. Feeling at the point I don't know what I am doing any longer. I wish to follow this boilerplate that makes total sense to me, but I'm stuck thinking I should go back to my simpler, although not production-ready approach.

Here is the whole traceback:

(.venv) PS D:\HDD_Code\SIGP3A_chest\SIGP3A> py manage.py check
Traceback (most recent call last):
  File "D:\HDD_Code\SIGP3A_chest\SIGP3A\manage.py", line 22, in <module>
    main()
  File "D:\HDD_Code\SIGP3A_chest\SIGP3A\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 460, in execute   
    output = self.handle(*args, **options)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\commands\check.py", line 76, in handle
    self.check(
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 487, in check     
    all_issues = checks.run_checks(
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks 
    new_errors = check(app_configs=app_configs, databases=databases)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver 
    return check_method()
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 480, in check
    for pattern in self.url_patterns:
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\utils\functional.py", line 49, in __get__        
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 696, in url_patterns    
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\utils\functional.py", line 49, in __get__        
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 689, in urlconf_module  
    return import_module(self.urlconf_name)
  File "C:\Users\fsoar\AppData\Local\Programs\Python\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
ModuleNotFoundError: No module named 'categories'

And here is the whole list of requirements in my virtual environment. For this job I decided to not create separated environments as it is experimental at this point.

asgiref==3.5.2
asttokens==2.0.5
backcall==0.2.0
black==22.6.0
certifi==2022.6.15
click==8.1.3
colorama==0.4.5
coverage==6.4.2
decorator==5.1.1
distlib==0.3.5
dj-database-url==1.0.0
Django==4.0.6
django-debug-toolbar==3.5.0
executing==0.9.1
factory-boy==3.2.1
Faker==13.15.1
filelock==3.7.1
flake8==5.0.3
gunicorn==20.1.0
ipython==8.4.0
isort==5.10.1
jedi==0.18.1
matplotlib-inline==0.1.3
mccabe==0.7.0
mypy-extensions==0.4.3
packaging==21.3
parso==0.8.3
pathspec==0.9.0
pickleshare==0.7.5
platformdirs==2.5.2
pluggy==1.0.0
prompt-toolkit==3.0.30
psycopg2==2.9.3
pure-eval==0.2.2
py==1.11.0
pycodestyle==2.9.0
pyflakes==2.5.0
Pygments==2.12.0
pyparsing==3.0.9
python-dateutil==2.8.2
python-decouple==3.6
pytz==2022.1
sentry-sdk==1.9.0
six==1.16.0
sqlparse==0.4.2
stack-data==0.3.0
toml==0.10.2
tomli==2.0.1
tox==3.25.1
traitlets==5.3.0
tzdata==2022.1
urllib3==1.26.11
virtualenv==20.16.2
wcwidth==0.2.5

I think I need to import something somewhere but I don't know exactly what, given that categories is already registered.


Solution

  • You need to use the actual app name for Django to know where the urls.py file to include is:

    # your installed app name
    INSTALLED_APPS = [
        ...
        'SIGP3A.Comps.categories'
    ]
    
    # your urls.py file
    path('categories/', include('SIGP3A.Comps.categories.urls'))