0

In the urls.py

from django.contrib import admin
from django.urls import path
import pandas as pd
import statsmodels.api as sm
urlpatterns = [path('admin/',admin.site.urls),]

When I comment out the 'import statsmodels.api as sm' line, the API is working fine and shows the django home page but when I include the statsmodels package it keeps on loading and throws timeout error. Please suggest what is going wrong. The packages are installed properly in the django environment.

Additional information: (Python version - 3.8.5, Django - 3.1.4, Ubuntu - 20.4)

Deployed this django API (installed apache2 and mod_wsgi with this django) in the Ubuntu EC2 instance and calling the django API from the local computer using Public IPv4 DNS. (Followed this site to deploy the django and apache - https://studygyaan.com/django/how-to-setup-django-applications-with-apache-and-mod-wsgi-on-ubuntu)

Theguy
  • 33
  • 9
  • import only what you need instead of the statsmodels.api, see https://www.statsmodels.org/dev/api-structure.html#direct-import-for-programs – Josef Dec 16 '20 at 13:01
  • @Josef Imported only the add_constant, Still facing the same problem. Does changing the Python version or Ubuntu version solve the problem? Any other solutions? – Theguy Dec 16 '20 at 14:07
  • `from statsmodels.tools.tools import add_constant` loads numpy, pandas and part of scipy but not much of statsmodels. – Josef Dec 16 '20 at 15:09
  • There should not be any large differences across versions and platforms. It's much faster if the computer still has the imports in cache memory in repeated usage. – Josef Dec 16 '20 at 15:14
  • There is no lag in importing other packages. Actually I'm importing more than 10 packages like pyodbc, sqlalchemy, datetime,etc. Only statsmodels package and sklearn packages are not working properly. I have uninstalled and reinstalled those packages again but the django website doesn't stop loading when I include statsmodels/sklearn but works fine when I take out these packages. – Theguy Dec 16 '20 at 15:29
  • statsmodels.api is huge and always slow to import. But after importing numpy, pandas and scipy.stats, `import statsmodels.regression.linear_model as lm` only adds 65 modules. Importing statsmodels.api after that loads 406 additional modules. (in my current versions of packages) – Josef Dec 16 '20 at 16:44

2 Answers2

0

The urls.py file is not a place for using statsmodels. You can use statsmodels in views.py, then define URL for that view in urls.py file as well.

dKen
  • 3,078
  • 1
  • 28
  • 37
Vu Phan
  • 594
  • 3
  • 8
  • I used it in the views.py as well. Still having the same problem. Other packages are working fine like pandas,numpy,os but the statsmodel.api is taking time. When I use 'import statsmodels as sm' its working but 'import statsmodels.api as sm' keeps on loading and loading. Any other solutions? Does downgrading my python version to 3.7 help? – Theguy Dec 16 '20 at 10:28
  • Got you, so I think your code which is using it takes a lot of time -> request time out. Try to resolve it as well – Vu Phan Dec 16 '20 at 10:32
  • I dont have any code at all. This is a default django app. The urls.py just uses the default django page using the admin.site.urls. Even when I used views.py the code just returns 'Success' as a HTTPResponse. – Theguy Dec 16 '20 at 10:36
0

Solved the problem finally (Check Out:Django Webfaction 'Timeout when reading response headers from daemon process')

Python C extension modules, like statsmodels/numpy, are known to cause timeouts when used under mod_wsgi. There's a clear explanation of the problem (direct from the author of mod_wsgi) available at https://serverfault.com/a/514251/109598

If that sounds like it might be the cause of your problem, then the solution is probably simple - add the following to your apache2.conf. Go to the apache2.conf in the etc/apache2/apache2.conf and add the below code at the end

WSGIApplicationGroup %{GLOBAL}

Be sure to restart your Apache instance after making that change

Theguy
  • 33
  • 9