【问题标题】:Django on Debian 9 - ImportError: No module named 'PROJECT.settings.py'Debian 9 上的 Django - ImportError:没有名为“PROJECT.settings.py”的模块
【发布时间】:2018-12-28 11:09:54
【问题描述】:

我正在尝试在 Google 计算引擎 Debian VM 实例上部署我的 Django 应用程序,我已经安装了 Python(3.6) 并设置了虚拟环境,然后克隆了在本地系统上运行良好的 Django 应用程序。 当我尝试运行python manage.py migrate 命令时,它返回错误:

ImportError: 没有名为“Fetchors.settings.py”的模块; 'Fetchors.settings' 不是一个包

这是我的Fetchors/wsgi.py

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(path)
if path not in sys.path:
    sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Fetchors.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

更新:我的目录结构是:

|-Fetchors
  |--Fetchors
   |--settings.py
  |--manage.py

这是我的manage.py

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Fetchors.settings.py")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

这里有什么问题?

提前致谢!

【问题讨论】:

  • 您的Fetchorssettings.py 文件吗?
  • 是的,它有,项目名称是Fetchors 然后主应用程序名称也是Fetchors。所以,我有Fetchors/Fetchors/settings.py
  • 你在某处添加了Fetchors.settings.py这一行吗?
  • wsgi.py中只提到Fetchors.settings

标签: python django debian django-migrations


【解决方案1】:

如果__init__.py 文件不可用,请在最里面的 Fetchors 文件夹中添加它。 所以你的目录结构应该如下所示:

|-Fetchors
  |--Fetchors
   |--__init__.py
   |--settings.py
  |--manage.py

这让 python 知道将 Fetchors 视为一个包。 __init__.py 可以是空文件。它应该已经存在于文件夹中。

【讨论】:

  • 您能否发布在 wsgi.py 中设置的路径变量的输出?如果项目文件夹不是路径的一部分,这很可能会导致问题。
猜你喜欢
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 2021-01-28
  • 2012-06-26
  • 2020-09-07
  • 2018-02-05
  • 2015-07-20
  • 2018-07-28
相关资源
最近更新 更多