Home > database >  ImportError executing wsgi script within Apache
ImportError executing wsgi script within Apache

Time:05-23

I've written a little Flask web app. I've configured it in Apache2 Ubuntu on my local machine, and it works under Apache.

Deploying to the remote server (Ubuntu/Apache2), the wsgi script fails with a ImportError

However, from the command line, executing the wsgi file does not create an error condition. Here's the error from the log, followed by an manual execution of the wsgi script:

[wsgi:error] [pid 414495] [client 174.52.77.157:51364] ImportError: No module named lakesidepy.app

root@localhost:/var/www/lakesidepy# ./lakesidepy.wsgi
root@localhost:/var/www/lakesidepy# 

This is the wsgi file:

#!/usr/bin/python3
import sys
sys.path.insert(0,"/var/www/")
from lakesidepy.app import app as application

This is the apache config:

<VirtualHost *:80>
  ServerName lakesidepy
  DocumentRoot /var/www/lakesidepy
  HostNameLookups off
  WSGIDaemonProcess lakesidepy user=www-data group=www-data threads=5
  WSGIScriptAlias / /var/www/lakesidepy/lakesidepy.wsgi
</VirtualHost>

the lakesidepy dir:

total 44
drwxr-xr-x  5 duane duane  4096 May 22 03:32 .
drwxrwsr-x 13 duane duane  4096 May 22 03:05 ..
-rwxr-xr-x  1 duane duane  3993 May 20 00:52 app.py
-rwxr-xr-x  1 duane duane   107 May 22 03:32 lakesidepy.wsgi
-rw-r--r--  1 root  root  12288 May 19 01:11 .lakesidepy.wsgi.swp
drwxr-xr-x  2 duane duane  4096 May 22 03:31 __pycache__
drwxr-xr-x  2 duane duane  4096 May 18 23:36 static
drwxr-xr-x  2 duane duane  4096 May 18 23:36 templates
-rw-r--r--  1 duane duane   285 May 22 02:53 test_script.py
duane@localhost:/var/www/lakesidepy$ 

Snippet from app.py:

from flask import Flask
.
.
.

app = Flask(__name__)

@app.route("/")
def input():
    return render_template('input.html')
...

CodePudding user response:

I found the problem.

The wsgi module on my local was properly configured for Python 3.
The wsgi module on the server was configured for Python 2.

This problem was fixed by replacing the wsgi module:

sudo apt-get install libapache2-mod-wsgi-py3

CodePudding user response:

in app.py file i would put

if __name__ == '__main__':
    app.run(host="localhost", port=3000, debug=True)

and in wsgi file

if __name__ == "__main__":
    app.run(port=3000)
  • Related