ModuleNotFound Error

Hello Django forum I am getting the ModuleNotFoundError: No module named ‘django_project’.
Whats hard for me to understand is that my django_project is the project root directory so I would think that python would not find the module django_project in the django_project folder.

I get this error when I run the command python manage.py collectstatic.

What steps have I taken to solve the problem?
I tried retracing my steps in the instructions for william s vincents Django for beginners to see if there are any errors.

I tried looking on freeCodeCamps articles: ModuleNotFoundError: no module named Python Error [Fixed]

I do not see any errors in my import statements so far. any advice?

The standard Django project directory structure as created by the startproject management command is to create an inner directory by the same name of the project as the project directory.

In other words, if you run django-admin startproject my_project, you’re going to create a directory named “my_project”, and within that directory, another directory named “my_project”.

It’s the contents of that inner directory that would be imported.

If you would like help with the specific error you are receiving, post the complete traceback from the error.

Thank you Ken I have gone ahead and made the change to the django_project folder so that there is an outer and inner folder that is named django project. inside the inner folder is everything else. The instructions in the book were telling me to do otherwise but its not getting results so this change does need to be made I think

I have made the change to the django_project folder. I am trying to get better by doing the fundamental tutorials, I have completed the polls app and now I am working on the django for beginners book and than possibly two scoops of django or freelancing. I am trying to get better. I do apologize for the many questions I have bookmarked all of them.

(.venv) andrewstribling@Andrews-MBP django_project % python3 manage.py collectstatic
Traceback (most recent call last):
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 255, in fetch_command
app_name = commands[subcommand]
~~~~~~~~^^^^^^^^^^^^
KeyError: ‘collectstatic’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/Users/andrewstribling/Desktop/code/message-board/django_project/django_project/manage.py”, line 22, in
main()
File “/Users/andrewstribling/Desktop/code/message-board/django_project/django_project/manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 442, in execute_from_command_line
utility.execute()
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 262, in fetch_command
settings.INSTALLED_APPS
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/conf/init.py”, line 102, in getattr
self._setup(name)
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/conf/init.py”, line 89, in _setup
self._wrapped = Settings(settings_module)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/andrewstribling/Desktop/code/message-board/.venv/lib/python3.11/site-packages/django/conf/init.py”, line 217, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “”, line 1204, in _gcd_import
File “”, line 1176, in _find_and_load
File “”, line 1126, in _find_and_load_unlocked
File “”, line 241, in _call_with_frames_removed
File “”, line 1204, in _gcd_import
File “”, line 1176, in _find_and_load
File “”, line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named ‘django_project’
(.venv) andrewstribling@Andrews-MBP django_project %

It looks like you’ve got your manage.py file in your inner django_project directory. That’s not where it would have been created by startproject and that’s not where it belongs. It also looks like you’ve got the inner django_project as your current directory, and that shouldn’t be the case either.

1 Like

I suppose in my debugging efforts I moved the files around. But moving the files around did not fix the problem. I suppose the lesson here is under no circumstances move the folders once the django start project command is run.

the error persists is thre any other advice you can offer me?

Let’s start by confirming what’s in each directory.

What is in the outer django_project directory?

What is in the inner django_project directory?

Confirm that you are in the outer django_project directory when you run manage.py.

And post the complete traceback of the error message you’re currently getting.

Outer directory: manage.py, .gitignore, db.sqlite3
Inner directory: posts, staticfiles folder,

init.py
.dockerignore
asgi.py
requirements.txt
settings.py
urls.py

So it seems to say no module named posts now AFTER I have made the outer directory folder and placed mostly everything in the inner directory. this makes sense because running pwd from the outer directory shows django_project and manage.py only. posts is the app folder which lives in the inner django_project folder.

(.venv) andrewstribling@Andrews-MBP django_project % python manage.py collectstatic
Traceback (most recent call last):
File “/Users/andrewstribling/Desktop/code/message-board/django_project/manage.py”, line 22, in
main()
File “/Users/andrewstribling/Desktop/code/message-board/django_project/manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/init.py”, line 442, in execute_from_command_line
utility.execute()
File “/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/init.py”, line 416, in execute
django.setup()
File “/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/init.py”, line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File “/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/apps/registry.py”, line 91, in populate
app_config = AppConfig.create(entry)
File “/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/apps/config.py”, line 193, in create
import_module(entry)
File “/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1030, in _gcd_import
File “”, line 1007, in _find_and_load
File “”, line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named ‘posts’
(.venv) andrewstribling@Andrews-MBP django_project %
wsgi.py

Your apps need to reside inside the outer directory at the same level as the inner project directory.

a new error appears! progress!

Thank you Ken!

Okay so the mindset for fixing these problems is:
-when I run the startproject command leave the folders alone don’t move them around.

  • if they are not finding the module error it is because when I run python manage.py there is no app folder or project folder. I have to move the folders accordingly.
  • and the apps DO NOT belong in the project folder, they belong in a separate folder on the same level as manage.py.

(.venv) andrewstribling@Andrews-MBP django_project % python manage.py posts
/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/db/models/base.py:366: RuntimeWarning: Model ‘posts.post’ was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.

That’s the key point. Once you’ve ensured that’s the case, then a module error is an indication of a different problem. Don’t try to over-generalize this solution based upon one problem.

Let’s start by checking your posts/models.py file. Ensure you don’t have two copies of the Post model in it.

Otherwise, there are a lot of different things to check.

I made the edit and it works!

Thank you Ken!