Home > Blockchain >  Flask with dash app, why WGSI can't import modules?
Flask with dash app, why WGSI can't import modules?

Time:12-15

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: ***************************************************

EDIT: In fact it seems WGSI won't import any module, tried with pandas:

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: ***************************************************

CodePudding user response:

I think what's happening is that you don't have an environment active when running the app that has dash and pandas installed.

You might have dash and pandas installed in an environment inside /home/mysite, but that doesn't mean when your execute flask run inside /var/www it automatically import those dependencies from your other environment.

I was able to get your code running by installing dash and pandas in an environment inside the folder pythonanywhere_com_wsgi.py is in.

CodePudding user response:

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

  • Related