Forums

WGSI can't import modules

Hello,

I'm trying to add dash instance to a flask app, but struggle with WGSI import.

WGSI doesn't want to import the dash module while it's installed and works well when I run directly the file Flask_app.py.

The /var/www/pythonanywhere_com_wsgi.py

import sys

# add your project directory to the sys.path
project_home = '/home/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application  # noqa

The /home/mysite/flask_app.py

from flask import Flask, render_template
import dash

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Flask!

# Set-up endpoint 1
app_1 = dash.Dash(name='app1', server=app, url_base_pathname='/app1/')
app_1.layout = html.H1('App 1')

The error:

Error running WSGI application
2021-12-14 16:16:26,591: ModuleNotFoundError: No module named 'dash'
2021-12-14 16:16:26,591:   File "/var/www/pythonanywhere_com_wsgi.py", line 16, in <module>
2021-12-14 16:16:26,591:     from flask_app import app as application  # noqa
2021-12-14 16:16:26,591: 
2021-12-14 16:16:26,592:   File "/home/mysite/flask_app.py", line 5, in <module>
2021-12-14 16:16:26,592:     import dash
2021-12-14 16:16:26,592: ***************************************************
2021-12-14 16:16:26,592: If you're seeing an import error and don't know why,
2021-12-14 16:16:26,592: we have a dedicated help page to help you debug: 
2021-12-14 16:16:26,592: https://help.pythonanywhere.com/pages/DebuggingImportError/
2021-12-14 16:16:26,592: ***************************************************

I tried with an other module and it's the same behavior:

2021-12-14 16:47:51,730: Error running WSGI application
2021-12-14 16:47:51,734: ModuleNotFoundError: No module named 'pandas'
2021-12-14 16:47:51,734:   File "/var/www/pythonanywhere_com_wsgi.py", line 17, in <module>
2021-12-14 16:47:51,734:     from flask_app import app as application  # noqa
2021-12-14 16:47:51,734: 
2021-12-14 16:47:51,735:   File "/home/mysite/flask_app.py", line 5, in <module>
2021-12-14 16:47:51,735:     import pandas
2021-12-14 16:47:51,735: ***************************************************
2021-12-14 16:47:51,735: If you're seeing an import error and don't know why,
2021-12-14 16:47:51,735: we have a dedicated help page to help you debug: 
2021-12-14 16:47:51,735: https://help.pythonanywhere.com/pages/DebuggingImportError/
2021-12-14 16:47:51,735: ***************************************************

Your web app is using a virtualenv. You need to install the modules you want to use in that virtualenv.

All the module seems installed (let me know if I'm wrong) and I still have the error

(venv) 18:41 ~/.virtualenvs/venv $ pip list --local
Package              Version
-------------------- ---------
Brotli               1.0.9
certifi              2021.10.8
charset-normalizer   2.0.9
click                8.0.3
dash                 2.0.0
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0
dataclasses          0.8
Flask                2.0.2
Flask-Compress       1.10.1
idna                 3.3
importlib-metadata   4.8.2
itsdangerous         2.0.1
Jinja2               3.0.3
MarkupSafe           2.0.1
numpy                1.19.5
pandas               1.1.5
pip                  21.1.2
plotly               5.4.0
python-dateutil      2.8.2
pytz                 2021.3
requests             2.26.0
setuptools           57.0.0
six                  1.16.0
tenacity             8.0.1
typing-extensions    4.0.1
urllib3              1.26.7
Werkzeug             2.0.2
wheel                0.36.2
zipp                 3.6.0

Make sure that the virtualenv that you're looking at is actually the one that is being used by your web app.

I was in the wrong environment, after installing all dependencies in the right on it works! Thanks

Glad to hear that you made it work!