module object is not callable error with Flask and Apache on Windows

Issue

I’ve recently set up apache server to serve my flask app on windows. Let me tell you about my setup.

  1. I’ve download apache through apache lounge. Have extracted it in C:\Apache24.
  2. I did install mod_wsgi in a virtual python 3.7.7
  3. Copied mod_wsgi configuration in the httpd.conf file
  4. created my_app.conf in the conf directory of Apache

But I’m getting the following error in the error.log file of apache…

[Fri Nov 11 18:44:00.985814 2022] [wsgi:error] [pid 9456:tid 1396] [client 127.0.0.1:58152] mod_wsgi (pid=9456): Exception occurred processing WSGI script 'J:/tools/pipecrew/ashish/workspace/tools/wsgi_scripts/shotgrid_ami_handler.wsgi'.
[Fri Nov 11 18:44:00.985814 2022] [wsgi:error] [pid 9456:tid 1396] [client 127.0.0.1:58152] TypeError: 'module' object is not callable\r
[Fri Nov 11 18:44:01.020776 2022] [wsgi:error] [pid 9456:tid 1392] [client 127.0.0.1:58153] mod_wsgi (pid=9456): Exception occurred processing WSGI script 'J:/tools/pipecrew/ashish/workspace/tools/wsgi_scripts/shotgrid_ami_handler.wsgi'., referer: http://fpservices.in/boom
[Fri Nov 11 18:44:01.020776 2022] [wsgi:error] [pid 9456:tid 1392] [client 127.0.0.1:58153] TypeError: 'module' object is not callable\r, referer: http://fpservices.in/boom

Here are the rest of my snippets

  1. Apache httpd.conf
LoadFile "J:/tools/rez_packages/python/3.7.7/platform-windows/python37.dll"
LoadModule wsgi_module "D:/virtual_python/python-3/mod_wsgi/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "D:/virtual_python/python-3/mod_wsgi"

# Include Flask File
Include conf/shotgrid_ami_handler.conf
  1. my flask app conf file in the conf directory of Apache
<VirtualHost *:80>
    ServerName fpservices.in
    WSGIScriptAlias / J:/tools/pipecrew/ashish/workspace/tools/wsgi_scripts/shotgrid_ami_handler.wsgi
    <Directory J:/tools/pipecrew/ashish/workspace/tools/shotgrid_ami_handler>
        WSGIScriptReloading On
        Require all granted
    </Directory>
</VirtualHost>
  1. and my wsgi file…
import sys

sys.path.insert(0, 'J:\\tools\\pipecrew\\ashish\\workspace\\tools')

from shotgrid_ami_handler import app as application
  1. and finally my flask app…
from flask import Flask, request, Response

app = Flask(__name__)

@app.route('/boom', methods=['GET', 'POST'])
def boom():
    return 'boom!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=4576, debug=True)

Can someone please point out why I’m getting ‘module’ object is not callable error here?

I’m trying to execute my flask routing function to execute through my Apache server. But I’m not getting enough error info in the apache error.log file to even try something.

Solution

It’s quite a very silly error. In my wsgi script I imported my main flask module as application object instead of the Flask object that’s inside the module. So my flask app has a module app which contains the Flask object app. So instead of doing this

from shotgrid_ami_handler import app as application

I should have done this

from shotgrid_ami_handler.app import app as application

Answered By – Vicspidy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published