[英]Django ModuleNotFoundError: No module named 'settings'
When I try to run 'python manage.py runserver' I get this error:当我尝试运行“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'
Here is my urls.py:这是我的 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)
Here is my models.py where I am just trying to import the local settings.py file I have.这是我的 models.py,我只是想导入我拥有的本地 settings.py 文件。 Not the main settings.py file, but the one I made locally for this app.不是主要的 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
When I try to run the server, it is telling me that it cant find settings.py, but settings.py is literally in the same app that models.py is in. Not sure why this is happening.当我尝试运行服务器时,它告诉我它找不到settings.py,但settings.py 确实在models.py 所在的同一个应用程序中。不知道为什么会这样。 Any help would be GREATLY appreciated.任何帮助将不胜感激。
Replace代替
import settings
with和
from django.conf import settings
If you have a custom settings.py, please update the question with the folder structure and the filenames.如果您有自定义 settings.py,请使用文件夹结构和文件名更新问题。
It's probably a problem with the folder structure of the python package, so basically import settings doesn't work because settings.py is not found from where the file is trying to import.这可能是python包的文件夹结构有问题,所以基本上导入设置不起作用,因为从文件尝试导入的位置找不到settings.py。
"""
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
Than in every file you just call this two lines:比在每个文件中你只需调用这两行:
import load_settings
settings = load_settings()
You will need to import it using the full path.您将需要使用完整路径导入它。 Try尝试
from ingest import settings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.