Home > Back-end >  Python3, Apache, mod-wsgi 'FileNotFoundError' is not defined
Python3, Apache, mod-wsgi 'FileNotFoundError' is not defined

Time:09-18

I have installed mod-wsgi from source, using Python3 (Apache 2.4.46 on an Amazon Linux 2 VM).

Every time I reload my wsgi daemon, I get the following error in my Apache error log:

Exception ignored in: <generator object path at 0x7f128e93fdd0>
Traceback (most recent call last):
  File "/usr/lib64/python3.7/importlib/resources.py", line 190, in path
NameError: name 'FileNotFoundError' is not defined

I think it's happening when the daemon goes down, as I also see the error when I take Apache down.

I run my project in a virtual environment in python3. This is my Apache config:

<VirtualHost *:443>
    <Directory /var/www/html/project/>
         Options  ExecCGI  FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
    </Directory>
    ErrorLog /home/ec2-user/apache_project_errors.log
    ServerName project.projtds.net
    DocumentRoot /var/www/html/project/
    WSGIDaemonProcess project threads=1 processes=2 python-home=/var/www/html/project/venv home=/var/www/html/project
    WSGIScriptAlias / /var/www/html/project/project.wsgi process-group=project
    WSGIScriptReloading On
</VirtualHost>

And my .wsgi is fairly simple:

from project import create_app
application = create_app()

All of this calling a flask app (project.py):

from flask import Flask


def create_app():
    app = Flask(__name__, instance_relative_config=False)

    @app.route("/")
    def home():
        import sys
        string = f"Python version {sys.version} Version info {sys.version_info}"
        return string

    return app

For now, I just check the python version since the error is typical of a python2 interpreter going over python3 code... But sys.version does show as 3.7 and I can run python3 specific calls in my project.

I checked how the wsgi module is loaded in Apache conf (/etc/httpd/conf.modules.d/wsgi.conf):
LoadModule wsgi_module modules/mod_wsgi.so

I dont see the error in other non wsgi projects running under the same Apache server. I have no idea how to fix the error. It doesn't prevent me from running my project but I'd love to know exactly where the error comes from and why.

Is there a way to get mod-wsgi debug logs? Is it something I need to do in my project? (flask, .wsgi, etc)

CodePudding user response:

I was missing a parameter:

    <Directory /var/www/html/project/>
         Options  ExecCGI  FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
    </Directory>

should have been:

    <Directory /var/www/html/jiraiop/>
     Options  ExecCGI  FollowSymlinks -SymLinksIfOwnerMatch
         Require all granted
         WSGIApplicationGroup %{GLOBAL}
    </Directory>

https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIApplicationGroup.html

Any WSGI applications in the global application group will always be executed within the context of the first interpreter created by Python when it is initialised, of the process handling the request.

I take it this is necessary to make sure the interpreter is python3. Not sure why it somehow had difficulties without it but... That fixes it.

  • Related