多个设置文件的问题。 Django/Python

Posted

技术标签:

【中文标题】多个设置文件的问题。 Django/Python【英文标题】:Problem with MULTPLE SETTINGS File. Django/Python 【发布时间】:2020-09-04 16:55:41 【问题描述】:

我正在尝试在我的 Windows 机器上运行一个 Django 项目。我收到 nomodulefounderror。该项目由 prod、Development 和 Test 的多个设置文件组成。 我正在使用虚拟环境,Python 2.7 和 Django 1.10 版本 我需要帮助才能在我的本地机器上运行这个项目。

Python manage.py runserver --settings=settings_dev_sai

它给我一个错误。这是我的错误日志


     python manage.py runserver --settings=bg.settings.settings_dev_sai
Traceback (most recent call last):
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "F:\brdge\bridge_sai\bridge\settings.py", line 38, in <module>
    from settings_dev_sai import *
ModuleNotFoundError: No module named 'settings_dev_sai'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 341, in run_from_argv
    connections.close_all()
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 225, in close_all
    for alias in self:
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 219, in __iter__
    return iter(self.databases)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 153, in databases
    self._databases = settings.DATABASES
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "F:\brdge\bridge_sai\bridge\settings.py", line 38, in <module>
    from settings_dev_sai import *
ModuleNotFoundError: No module named 'settings_dev_sai'


ma​​nage.py 文件

import os
import sys

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

错误日志已更新

 Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "F:\brdge\env1\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
        utility.execute()
      File "F:\brdge\env1\lib\site-packages\django\core\management\__init__.py", line 316, in execute
        settings.INSTALLED_APPS
      File "F:\brdge\env1\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
        self._setup(name)
      File "F:\brdge\env1\lib\site-packages\django\conf\__init__.py", line 41, in _setup
        self._wrapped = Settings(settings_module)
      File "F:\brdge\env1\lib\site-packages\django\conf\__init__.py", line 97, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
        __import__(name)
    ImportError: No module named settings_dev_sai

【问题讨论】:

【参考方案1】:

我想我需要回答我的问题,nomodulefounderror 的问题,因为我的 Postgres 数据库中的数据不完整。我已将 .sql 文件转储到我的经理提供的 Postgres 数据库中,然后根据数据库名称配置设置文件并运行命令 python manage.py runserver --settings=bg.settings_dev_sai。效果很好。

【讨论】:

【参考方案2】:

您的项目目录名称似乎是'bg',所以我认为在导入模块时应该这样做

from bg import settings_dev_sai

from bg.settings_dev_sai import *

还要确保 settings_dev_sai 在同一目录中 'bg'

【讨论】:

嗨,@NitinGavai 感谢您回答我的问题。我确实尝试在设置文件中添加 bg.settings_dev_sai 。但它让我导入错误。我已经上传了错误日志。请看一次,让我知道该怎么做。先生 你试过python manage.py runserver --settings=bg.settings_dev_sai吗? 是的,我确实尝试使用该服务器运行。但发生了同样的错误【参考方案3】:

我认为你只需要运行它

python manage.py runserver --settings=bridge.settings_dev_sai

因为 bridge.settings 是一个文件而不是一个包

【讨论】:

嗨@Cauane。我尝试运行 python manage.py runserver。它把我扔了ImportError: no module named settings_dev_sai。我不知道为什么会出现这个错误

以上是关于多个设置文件的问题。 Django/Python的主要内容,如果未能解决你的问题,请参考以下文章

Django - Python 3 - “AssertionError:一个模型不能有多个 AutoField。”

Django/Python:处理多个多向外部 API 的外部 ID 的最佳实践/建议

(Django python)使用多个“for循环”进行渲染[重复]

无法在 django python 中提供诸如 css、js 之类的静态文件

Django/Python 在 DateTimeField 上设置最小/最大日期值

django python 日期时间设置为午夜