I’m trying to recreate a simple PHP\Symfony project in Python\Django as a learning experience. My attempt to work with the database API stumbles with “TypeError: the ‘package’ argument is required to perform a relative import for ‘.diet_project.settings.py’” as seen below. Please advise.
With $env:DJANGO_SETTINGS_MODULE="diet_project.settings.py"
I get …
>>> from meals.models import Food
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "G:\workspace\python-land\diet\meals\models.py", line 5, in <module>
class Food(models.Model):
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\db\models\base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready
settings.INSTALLED_APPS
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 92, in __getattr__
self._setup(name)
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 79, in _setup
self._wrapped = Settings(settings_module)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 190, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'diet_project.settings.py'; 'diet_project.settings' is not a package
With %env:DJANGO_SETTINGS_MODULE=".diet_project.settings.py"
I get…
>>> from meals.models import Food
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "G:\workspace\python-land\diet\meals\models.py", line 5, in <module>
class Food(models.Model):
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\db\models\base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready
settings.INSTALLED_APPS
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 92, in __getattr__
self._setup(name)
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 79, in _setup
self._wrapped = Settings(settings_module)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\conf\__init__.py", line 190, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\importlib\__init__.py", line 121, in import_module
raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.diet_project.settings.py'
Background:
The environment is Windows 10, Visual Studio Code, powershell
Database is MySQL. Migrations all successful, tables populated
Project structure
diet
diet_project
...
settings.py
meals
...
models.py
models.py
:
class Food(models.Model):
food_name = models.CharField(max_length=100)
def __str__(self):
return self.food_name
class Meta:
db_table = "food"
ordering = ['food_name']
...
settings.py
:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'meals',
"bootstrap5",
]
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'diet_py',
'USER': 'username',
'PASSWORD': 'password',
'HOST':'localhost',
'PORT':'3306',
}
}