Import conflict when running flask app with apache mod_wsgi on windows

I am permit you to ask you about a problem that I have with hosting flask application with your portage of mod_wsgi on windows

I have two flask application and only one can be alive a the same times due to conflict in import

ie : If a request application 1 I have a response Then if I request application 2 I have internal server error with error in log ... Then if I restart apache and I request application 2 I have a response but if I request application 1 I have the same internal server error If I comments some import like numpy both application can be alive at the same time

Any help would be appreciated if you have any idea or link or answer to about this problem?

My installation is describe below

Thanks by advance for tour times and your works

Alexandre

LOG of the error mod_wsgi (pid=4936): Failed to exec Python script file 'D:/exec/Apache24/htdocs/wsgi/api_test_2.wsgi'. mod_wsgi (pid=4936): Exception occurred processing WSGI script 'D:/exec/Apache24/htdocs/wsgi/api_test_2.wsgi'. Traceback (most recent call last): File "D:/exec/Apache24/htdocs/wsgi/api_test_2.wsgi", line 3, in from api_test_2 import app as application File "D:/exec/wsgi_api/api_test_2\api_test_2.py", line 2, in import numpy File "c:\python\python36\lib\site-packages\numpy\__init__.py", line 142, in from . import core File "c:\python\python36\lib\site-packages\numpy\core\__init__.py", line 16, in from . import multiarray File "c:\python\python36\lib\site-packages\numpy\core\multiarray.py", line 12, in from . import overrides File "c:\python\python36\lib\site-packages\numpy\core\overrides.py", line 46, in """) RuntimeError: implement_array_function method already has a docstring

#---------------------------------
# file : D:/exec/wsgi_api/api_test_1/api_test_1.py    
#---------------------------------
from flask import Flask, jsonify,render_template, request, make_response
import numpy
app = Flask(__name__)
@app.route('/')
def home():
    resp = make_response("hello from 1", 200)
    resp.headers['Content-Type'] = 'charset=utf-8'
    return resp
#---------------------------------        

#---------------------------------
# file : D:/exec/wsgi_api/api_test_2/api_test_2.py    
#---------------------------------
from flask import Flask, jsonify,render_template, request, make_response
import numpy
app = Flask(__name__)    
@app.route('/')
def home():
    resp = make_response("hello from 2", 200)
    resp.headers['Content-Type'] = 'charset=utf-8'
    return resp
if __name__ == '__main__':
    app.run(host='127.0.0.1', port=36000)
#---------------------------------

I have this two wsgi file in appache httpdocs

#---------------------------------
# file : D:/exec/Apache24/htdocs/wsgi/api_test_1.wsgi
#---------------------------------
import sys 
sys.path.append('D:/exec/wsgi_api/api_test_1/') 
from api_test_1 import app as application
#---------------------------------


#---------------------------------
# file : D:/exec/Apache24/htdocs/wsgi/api_test_2.wsgi
#---------------------------------
import sys 
sys.path.append('D:/exec/wsgi_api/api_test_1/') 
from api_test_1 import app as application
#---------------------------------


#---------------------------------
In D:/exec/Apache24/conf/httpd.conf i add the line
#---------------------------------
WSGIScriptAlias /api_test_1 "D:/exec/Apache24/htdocs/wsgi/api_test_1.wsgi"
WSGIScriptAlias /api_test_2 "D:/exec/Apache24/htdocs/wsgi/api_test_2.wsgi"
#---------------------------------

I had a similar issue in a project. In my case, I had to add WSGIApplicationGroup %{GLOBAL} inside the .conf file of my site.

This is the documentation where I found the information: https://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#sub-interpreter-being-used

Before installing mod_wsgi, we need to get the code for our Flask application into the deployment machine and then create a python3 virtualenv (say flask-app-env) for the application.

I had a discussion on the mod_wsgi mailing list and I had the same response The explanation is numpy doesn't work in Python sub interpreters as the C extension modules are not implement properly to allow that, thus for mod_wsgi you can only use numpy in the main interpreter context, forced by the 'WSGIApplicationGroup %{GLOBAL}' directive

This is linked with the subject https://github.com/numpy/numpy/issues/3961

Instead of downloading this "mod_wsgi-windows-4.4.12.tar.gz" this file and looking for the right mod_wsgi.so file and copy it to my "C:\Users\lucid\VS16\Apache24\modules" and pasting .so file, I downloaded mod_wsgi using "pip install mod_wsgi"and after i ran "mod_wsgi-express module-config" and copied the below output to httpd.conf at last line

Try putting this in your WSGI configuration file:

single-interpreter = true

First of all, install python, pip and mod_wsgi: apt install python3-dev python3-pip libapache2-mod-wsgi. Restart apache2 service and verify that wsgi module is loaded: sudo apache2ctl -M|grep -i wsgi. Create a projectname.wsgi file in your project: import sys sys.path.insert(0, '/path/to/project') from app import app as application

Although Flask has nice documentation regarding the deployment of flask application in apache server using mod_wsgi, it’s very normal to be stuck in and between the process and end up in some silly errors. So in this post, I will guide you through deploying flask application using mod_wsgi.

If you are using the Apache webserver, consider using mod_wsgi. Watch Out Please make sure in advance that any app.run() calls you might have in your application file are inside an if __name__ == '__main__': block or moved to a separate file.

A flask app runs on port 5000 and if we serve the web page through Flask, then it's fine, but no one wants to go to a website with a Port number. So because of that, we will run the Flask app behind Apache so that we can simpley hit our Pi's IP address and control our LED.

Comments
  • What file exactly are you referring by .conf can you please provide the complete path. Thanks!
  • And if you meant httpd.conf where exactly do I need to add it, which section I mean.
  • @lorenzo, I was stuck here for 3 days. Thank you so much.
  • The problem is the same on linux.