没有名为 foo.settings 的模块 Django 导入错误

No module named foo.settings Django Import Error

我在第一次克隆存储库后遇到 Django 导入错误,我查看了之前的所有问题,但无法弄清楚我的问题是什么...

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

但是我的 settings.py 文件在 foo 目录中?

Project

app1

app2

foo

settings.py

wsgi.py

urls.py

app3

manage.py

我真的很困惑。这是我的 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)

为了 Python 将目录识别为模块,它需要包含一个名为 __init__.py 的文件。此文件可以为空,也可以包含模块顶层代码。

来自Python documentation

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

相关 SO 问题:What is __init__.py for?