Django ModuleNotFoundError:没有名为'settings'的模块

时间:2017-05-27 23:14:10

标签: python django

问题

当我尝试运行'python manage.py runserver'时,我收到此错误:

    Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x1068ddc80>
Traceback (most recent call last):
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/utils/autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/aricliesenfelt/.virtualenvs/django/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/aricliesenfelt/Desktop/scratch/django/django_tutorials/supplyai/ingest/models.py", line 5, in <module>
    import settings.py
ModuleNotFoundError: No module named 'settings'

这是我的urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^ingest/', include('ingest.urls'))
]

urlpatterns = format_suffix_patterns(urlpatterns)

这是我的models.py,我只是想导入我拥有的本地settings.py文件。不是主要的settings.py文件,而是我在本地为此应用程序制作的文件。

from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL

import settings

DeclarativeBase = declarative_base()

def db_connect():
    return create_engine(URL(**settings.DATABASE))

def create_data_table(engine):
    DeclarativeBase.metadata.create_all(engine)

class Data(DeclarativeBase):
    __tablename__ = 'data'

    id = Column(Integer, primary_key=True)
    shipper_name = Column('shipper_name', String)
    seller_location = Column('seller_location', String)
    buyer_location = Column('buyer_location', String)
    product_category = Column('product_category', String)
    order_created_date = Column('order_created_date', String)

    def __init__(self, id, shipper_name, seller_location, buyer_location, product_category, order_created_date):
        self.id = id
        self.shipper_name = shipper_name
        self.seller_location = seller_location
        self.buyer_location = buyer_location
        self.product_category = product_category
        self.order_created_date = order_created_date

当我尝试运行服务器时,它告诉我它无法找到settings.py,但settings.py实际上是在models.py所在的应用程序中。不确定为什么会发生这种情况。任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:2)

替换为import settings from django.conf import settings

答案 1 :(得分:-1)

"""
Load Settings from django
"""
def setup_settings():
    import os
    import django

    # Connect to the Django Database
    script_path = os.path.dirname(__file__)
    os.environ['DJANGO_SETTINGS_MODULE']='<<YOUR_PROJECT>>.settings'
    django.setup()

def load_settings():

    from django.core.exceptions import ImproperlyConfigured

    try:
        from django.conf import settings as settings
        settings.BASE_DIR <<<< Try to access one your settings
    except (ImproperlyConfigured, ModuleNotFoundError) as ex:
        print("Settings not loaded yet.. Load it now.")
        setup_settings()
        from django.conf import settings as settings
        settings.BASE_DIR
        print("BASE_DIR (%s) loaded." % settings.BASE_DIR)
    except Exception as ex:
        print("Could not load settings (%s)" % ex)

    if not hasattr(settings, 'BASE_DIR'):
        print("Could not find BASE_DIR in settings! ERROR!")
        sys.exit(1)

    return settings

在每个文件中,您只需将这两行称为:

import load_settings
settings = load_settings()

答案 2 :(得分:-3)

您需要使用完整路径导入它。尝试

from ingest import settings