TypeError:“模块”对象在 django 4 中不可迭代

问题描述 投票:0回答:2

类型错误:“模块”对象在 django 4 中不可迭代

我收到上述错误,它已经持续了足够长的时间,此时我真的需要帮助。 我使用 pickle 加载 ML 模型,使用 Django 获取用户输入。下面是错误, 我的 urls.py 文件和views.py 文件。

任何帮助将不胜感激。

所有代码都在我的 GitHub 中。

******** 启动服务器时,我收到此错误消息 ********

(python10_env) D:\Online Drives\MDigital\CIT-Letures\python10_env\smart_health_consult>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\urls\resolvers.py", line 634, in url_patterns
    iter(patterns)
**TypeError: 'module' object is not iterable**

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\management\base.py", line 438, in check
    all_issues = checks.run_checks(
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\checks\registry.py", line 77, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\checks\urls.py", line 67, in _load_all_namespaces
    namespaces.extend(_load_all_namespaces(pattern, current))
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\Online Drives\MDigital\CIT-Letures\python10_env\lib\site-packages\django\urls\resolvers.py", line 642, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'ml_dp_model.urls' from 'D:\\Online Drives\\MDigital\\CIT-Letures\\python10_env\\smart_health_consult\\ml_dp_model\\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

这是ml_dp_model>urls.py **** urls.py *****

from django.urls import path
from . import views

#from .views import index
#from .views import predict

urlpartterns = [
    path('', views.index, name='index'),    
    path('result.html', views.predict, name='predict')
    
]

这是ml_dp_model>views.py ********* 浏览次数.py *********

from django.shortcuts import render
# Testing if context can solve circular refference issue.
from django.template import context
# Model related imports
import pandas as pd #install pandas
import pickle
import numpy as np #  helps to manipulate the data

# Create your views here.
def index(request):
    return render(request, 'index.html')

# importing the models using pickle.
    #Loading Naive Bayes Pickle  Model load method 1
nb_pickle = open('./models_store/final_nb_model.pickel','rb+')
nb_model = pickle.load(nb_pickle)
    #Loading RandomForest Pickle Modal load method 1
rf_pickle = open('./models_store/final_rf_model.pickel', 'rb+')
rf_model = pickle.load(rf_pickle)
    #Loading Scala Vector Machine  Pickle  Model load method 2
svm_model = pickle.load(open('./models_store/final_svm_model.pickel', 'rb+'))

# Disease prediction Function:
def predict(request):
    if request.method=='POST':
        symptom_index = {}
        symptom_index['symptom1'] =float(request.POST.get('symptom1')) # Add data in string format to the dictionary
        symptom_index['symptom2'] =float(request.POST.get('symptom2'))
        symptom_index['symptom3'] =float(request.POST.get('symptom3'))
        user_symptoms = pd.DataFrame({'X':symptom_index}).transpose() #think about changing dictionary to list at this line.
        # Using pickle model() to predict
        nb_prediction = nb_model.predict(user_symptoms)[0]
        rf_prediction = rf_model.predict(user_symptoms)[0]
        svm_prediction = svm_model.predict(user_symptoms)[0]
                
        '''
        Making final prediction by taking mode of all predicitions
        '''
        final_prediction = np.mode([rf_prediction, nb_prediction, svm_prediction])
        
        predicted  =  final_prediction

    return render(request,'result.html',{'results':predicted} )

python-3.x django scikit-learn pickle
2个回答
3
投票

你的 urls.py 中有一个拼写错误

urlpartterns
应该是
urlpatterns
:)


0
投票

尝试在 cmd 提示符下运行 python manage.py runserver 命令时遇到同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.