Ruth Young Ruth Young - 4 months ago 18
Python Question

No module named foo.settings Django Import Error

I'm gettings a Django import error after just cloning a repo for the first time, and I've looked at all previous questions and can't figure out what my problem is...

Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 302, in execute
settings.INSTALLED_APPS
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 99, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named foo.settings


But I have my settings.py file in the foo directory?



Project


app1

app2

foo


settings.py

wsgi.py

urls.py


app3

manage.py




I am really confused. Here is my manage.py

# !/usr/bin/env python
import os
import sys


if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "foo.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

Answer Source

For Python to recognise a directory as a module, it needs to contain a file named __init__.py. This file can be empty, or can contain module top-level code.

From the Python documentation:

The __init__.py files are required to make Python treat the directories as containing packages

Related SO Question: What is __init__.py for?