Broke project adding and deleting new app.: Module not found error

I tried adding and removing a second app in my project in VS Code and wound up really messing things up. Django is installed and verified but when I try to runserver I get:

(.venv) (base) b@Mac Resilience_R6 % python manage.py runserver     
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 126, in inner_run
    autoreload.raise_last_exception()
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 394, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/apps/config.py", line 193, in create
    import_module(entry)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'ISO22301django'

Did you forgot to remove it from INSTALLED_APPS?

I did. The ISO22301 is the current app, not the old one.

Please post your INSTALLED_APPS setting here. (I’ve got a hunch you’re missing a comma in the list.)

Bingo. That was it.

It looks like this was the clue in the error message:

  File "/Users/b/Documents/Work/Python Projects/Dashboard/Resilience_R6/.venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)

Which brings me to what started this mess. I wanted to rename the app all the references I found had conflicting guidance. It seems I can rename all the references to ISO22301 to the new name, but the SQLLite db tables are still named ISO22301_table. Is there a good set of instructions on how to rename, or am I better off just creating a new app in the project , migrating the files, and then deleting the old app?

If it can be too problematic I can live with the old name.

If I had to do this, I would create my new app with all the models having the db_table attribute referencing the old name. I would run a makemigrations / migrate to verify that nothing was changed. Then remove the db_table attribute and rerun the makemigrations / migrate. It should properly change all the table definitions. (At least it did in one of my test systems.)

Actually, what caught my eye was this:

combined with this:

It seemed like a good guess that django being concatenated on ISO22301 was an indication of an implicit string literal concatenation in the INSTALLED_APPS setting.

1 Like

Thanks.

I’ll give it a try with a clone of the project. Two questions:

  1. Where do I find the db_table attribute? From the docs, it was created when I created the app as I did not override it with class Meta in models.py

  2. I have two folders, ISO22301 and ResilienceRadarR5 whose names were holdovers from the project I used the new one on. As long as I change all the references, such as in asgi, wigs, settings, templates, etc., to their new names, and update the database I should be good to go?

You would add it yourself if it doesn’t already exist.

Best that I can tell, yes. (Some things may not need to be changed, but you might want to change them for consistency. I’m specifically thinking of possible URL definitions and relative static file and template file directory references.)

1 Like