TypeError: 'module' object is not iterable - django 4

3,605 views
Skip to first unread message

muwaga micheal

unread,
Jan 31, 2022, 3:53:03 AM1/31/22
to Django users
Hi 
Team,
 I am getting the above error, it has persisted long enough than at this point i really need help.
I am using pickle to load an ML model, POST to get user input. Below is the error, my urls.py file and the views.py file. 
Any Help will be highly appreciated.

******* Error Message *******

(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.

******************************

****  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')
   
]
******************

********* views.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} )

****************************
 Am running python 3.10  and Django 4.0


muwaga micheal

unread,
Feb 1, 2022, 5:52:45 AM2/1/22
to Django users
Reply all
Reply to author
Forward
0 new messages