Why does the package name need to be included in this import statement? [duplicate]

To set things up, here's my directory structure for my Django project:

RecallThatMovie
|
|-- RecallThatMovie
|   |
|   |-- __pycache__ (directory)
|   |
|   |-- __init__.py
|   |
|   |-- asgi.py
|   |
|   |-- myconfig.py
|   |
|   |-- settings.py
|   |
|   |-- urls.py
|   |
|   |-- wsgi.py
|
|-- db.sqlite3
|
|-- manage.py

And I have this code at the top of the settings.py file:

"""
Django settings for RecallThatMovie project.

Generated by 'django-admin startproject' using Django 5.1.5.
"""

import myconfig
from pathlib import Path


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = myconfig.SECRET_KEY

When I run python manage.py I get this error:

  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\jaull\Documents\RecallThatMovie\RecallThatMovie\settings.py", line 13, in <module>
    import myconfig
ModuleNotFoundError: No module named 'myconfig'

However when I change from: import myconfig to this: import RecallThatMovie.myconfig:

And the SECRET_KEY line to this: SECRET_KEY = RecallThatMovie.myconfig.SECRET_KEY

Then the Django server works when I do: python manage.py runserver

Why is that? I mean settings.py and myconfig.py are in the same directory- or more specifically the same package.

Вернуться на верх