How to resolve "the 'package' argument is required to perform a relative import"?

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',
	}
}

Please show the command you’re using to start the Python shell.

Also review the work you would have done in step 2 of the official Django tutorial.

I think I’m well off the beaten path here. For one, I built the project using this because I was flummoxed by the tutorial. With that structure I was able to make some progress.

My second step off the path was that I was using (.venv) PS G:\workspace\python-land\diet> python to launch the shell. Looking back at the tutorial, I see instead (.venv) PS G:\workspace\python-land\diet> python manage.py shell. When I tried that just now I get:

Traceback (most recent call last):
  File "G:\workspace\python-land\diet\manage.py", line 22, in <module>
    main()
  File "G:\workspace\python-land\diet\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "G:\workspace\python-land\diet\.venv\Lib\site-packages\django\core\management\__init__.py", line 386, in execute
    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'

Looks like my spaghetti route ends up in the same place.

First, remove whatever environment variables you might have set for this. The purpose of the manage.py file is to use the project’s default.

What’s your directory structure in your diet directory?

Did you use the startproject command to initialize the project? If not that, how?

So I axed the DJANGO_SETTINGS_MODULE as suggested. The “meat” of the diet directory appears near the top of my original message.

Most important, though, is that by clearing the environment variable & using the “correct” shell command I get this:

(.venv) PS G:\workspace\python-land\diet> python manage.py shell        
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from meals.models import Food
>>> 

which, I believe, is where I needed to go.

Big thanks for getting me back on track.

1 Like