Home > Mobile >  Django - No module named 'mywebsite'
Django - No module named 'mywebsite'

Time:08-21

I have a django app that I downloaded from a Github repo where the main app is not called "mywebsite" (like instructed in the django tutorial).

Everytime I run python manage.py runserver i get the following error.

Conceptually I understand the error, but practically I don't. The issue can't be that I have always need to name my repo "mywebsite" right?

This is the current repo structure.

enter image description here

Traceback (most recent call last):
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 74, in execute
    super().execute(*args, **options)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 81, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/opt/homebrew/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mywebsite'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/Yggdrasil/yggdrasil/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/core/management/base.py", line 415, in run_from_argv
    connections.close_all()
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/connection.py", line 84, in close_all
    for conn in self.all(initialized_only=True):
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/connection.py", line 76, in all
    return [
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/connection.py", line 73, in __iter__
    return iter(self.settings)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/db/utils.py", line 148, in configure_settings
    databases = super().configure_settings(databases)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/utils/connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/nicolamacchitella/Documents/GitHub/familytree/.env/lib/python3.9/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/opt/homebrew/Cellar/[email protected]/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mywebsite'

CodePudding user response:

Most likely it's because of your project name. You have 2 options at the moment: (1) rename back to mywebsite or (2) rename all occurences of "mywebsite" in your project to your project name.

If you so choose (2), check your project settings and you'll see lot of variables like this ROOT_URLCONF = 'mywebsite.urls' for which you have to change to ROOT_URLCONF = 'yggdrasil.urls'. And you must also do that in all your apps and everywhere else that uses mywebsite as root reference.

CodePudding user response:

I've gone through the code in the repo and apparently everything seems to be in order.

Looking then again at the error OP gets, it starts in line

mod = importlib.import_module(self.SETTINGS_MODULE)

which then searches for a file using a path different than Windows, File "/opt/homebrew/Cellar/python.... This shows that OP is having a problem importing the settings module.

After some digging, found this page in Django docs that states

If this variable isn’t set, the default wsgi.py sets it to mysite.settings, where mysite is the name of your project. That’s how runserver discovers the default settings file by default.

To fix it, try enforcing the value for in your wsgi.py file, like

os.environ["DJANGO_SETTINGS_MODULE"] = "yggdrasil.settings"
  • Related