【solved】django-admin causes ModuleNotFoundError of setting files

When I do this:

export DJANGO_SETTINGS_MODULE=KFIPtutorial.setting_local
django-admin dumpdata -a

ModuleNotFoundError occurred.

Traceback (most recent call last):
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 412, in execute
    self.check()
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/core/management/base.py", line 438, in check
    all_issues = checks.run_checks(
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/core/checks/registry.py", line 77, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/core/checks/caches.py", line 62, in check_file_based_cache_is_absolute
    for alias, config in settings.CACHES.items():
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 84, in __getattr__
    self._setup(name)
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 71, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/akakatsu/.local/lib/python3.10/site-packages/django/conf/__init__.py", line 179, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/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 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 'KFIPtutorial'

Any other django-admin commands such as makemessages is same.

directory structure:

KFIPtutorial (project name)
│
├── manage.py
├── KFIPtutorial
│   ├── __init__.py
│   │
│   ├── setting_base.py
│   ├── setting_local.py      (import setting_base.py)
│   ├── setting_production.py (import setting_base.py)
│   │
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
├── Procfile
├── README.md
├── blackjack (my app)
│   ├── static
│   ├── templates
│   ├── locale
│   ├── migrations
│   ├── fixtures
│   │
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
│   │
│   ├── node_modules
│   ├── src
│   │
│   ├── babel.config.json
│   ├── package-lock.json
│   ├── package.json
│   └── webpack.config.js
├── db.sqlite3
├── geckodriver.log
├── locale
├── requirements.txt
└── staticfiles

Runnning the following command, the contents of setting_local.py was output successfully.

python manage.py diffsettings

and, “python manage runserver” also success.

Django version 4.0.2, using settings 'KFIPtutorial.setting_local'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

I can’t figure out the reason why django-admin command cause that error.
I would appreciate it if someone tell me it.

Is your current directory your project directory when you try to run django-admin? If not, try it from there.

1 Like

Thank you for replying. Yes, It is.
Additionally, I tried in the parent directory of my project, and in KFIPtutorial/KFIPtutorial.
But same error occurred :sob:

Try adding your project directory to PYTHONPATH.
(e.g. export PYTHONPATH=/home/akakatsu/KFIPtutorial, or whatever your project path is)

1 Like

fantastic!
The error disappeared by following your instruction!
Thank you very much for your help. :blush:

hi @KenWhitesell I getting same issue and tried the solution provided.
Im trying to run daphne for websocket but its failing and throwing same error like above with my project directory name. im attaching daphne error log for more info below.

 Traceback (most recent call last):
  File "/home/administrator/people/peoples/venv/bin/daphne", line 8, in <module>
    sys.exit(CommandLineInterface.entrypoint())
  File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
  cls().run(sys.argv[1:])
 File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
application = import_by_path(args.application)
 File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
   target = importlib.import_module(module_path)
 File "/usr/lib/python3.8/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 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "/home/administrator/people/peoples/./peoples/asgi.py", line 6, in <module>
     from chat.consumers import ChatRoomConsumer
 File "/home/administrator/people/peoples/./chat/consumers.py", line 6, in <module>
  from people.models import User
 File "/home/administrator/people/peoples/./people/models.py", line 5, in <module>
Apr 18 11:48:03 35651.hostserv.eu python[119025]:     from django.contrib.auth.models import AbstractUser
 File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
  from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
 File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
Apr 18 11:48:03 35651.hostserv.eu python[119025]:     class AbstractBaseUser(models.Model):
File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/db/models/base.py", line 107, in __new__
   app_config = apps.get_containing_app_config(module)
  File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
  File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/apps/registry.py", line 134, in check_apps_ready
 settings.INSTALLED_APPS
  File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
  self._setup(name)
File "/home/administrator/people/peoples/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 57, in _setup
   raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. . You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This topic has already been marked as resolved. I would suggest you open a new topic for this issue. Please repost the traceback there, along with a description about how your environment is set up to run Daphne, and how you’re actually running it.