Search code examples
flaskvirtualenvpythonanywheresite-packages

pythonanywhere virtualenv packages not importing for flask app


I'm new to pythonanywhere and I'm trying to set up a flask web app.

I'm using a virtualenv:

/home/SpaceMeerkat/.virtualenvs/flaskenv

When I activate the venv and run:

from werkzeug.urls import url_quote
from flask import Flask, g
import pandas as pd

...the packages are imported with no errors.

However when I run my web app using a WSGI script:

import sys

path = '/home/SpaceMeerkat/4211-IOMS/thredspace'
if path not in sys.path:
    sys.path.append(path)
#
from __init__ import create_app  # noqa
app = create_app()

Which invokes the script:

import os
from flask import Flask, g
from flask_cors import CORS
import pandas as pd

# =============================================================================
# Setup app config
# =============================================================================

def create_app(test_config=None):
    
    app = Flask(__name__, instance_relative_config=True)
    CORS(app)
    
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'thredspace.sqlite'),
    )
    
    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)
    else:
        app.config.from_mapping(test_config)
        
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    
    from . import home
    app.register_blueprint(home.bp)
    
    from . import db
    db.init_app(app)
    
    from . import auth
    app.register_blueprint(auth.bp)
    
    from .templates.creation_suite import project_management
    app.register_blueprint(project_management.bp)
    
    return app

I'm getting errors like these:

2023-11-24 15:34:44,071: Error running WSGI application
2023-11-24 15:34:44,078: ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (/home/SpaceMeerkat/.virtualenvs/flaskenv/lib/python3.10/site-packages/werkzeug/urls.py)
2023-11-24 15:34:44,078:   File "/var/www/spacemeerkat_pythonanywhere_com_wsgi.py", line 79, in <module>
2023-11-24 15:34:44,079:     from thredspace.main_flask_app_file import app as application  # noqa
2023-11-24 15:34:44,079: 
2023-11-24 15:34:44,079:   File "/home/SpaceMeerkat/4211-IOMS/thredspace/__init__.py", line 14, in <module>
2023-11-24 15:34:44,079:     from flask import Flask, g
2023-11-24 15:34:44,079: 
2023-11-24 15:34:44,079:   File "/home/SpaceMeerkat/.virtualenvs/flaskenv/lib/python3.10/site-packages/flask/__init__.py", line 5, in <module>
2023-11-24 15:34:44,079:     from .app import Flask as Flask
2023-11-24 15:34:44,079: 
2023-11-24 15:34:44,079:   File "/home/SpaceMeerkat/.virtualenvs/flaskenv/lib/python3.10/site-packages/flask/app.py", line 30, in <module>
2023-11-24 15:34:44,079:     from werkzeug.urls import url_quote

and

2023-11-24 15:49:54,151: Error running WSGI application
2023-11-24 15:49:54,152: ModuleNotFoundError: No module named 'pandas'
2023-11-24 15:49:54,152:   File "/var/www/spacemeerkat_pythonanywhere_com_wsgi.py", line 79, in <module>
2023-11-24 15:49:54,152:     from thredspace.main_flask_app_file import app as application  # noqa
2023-11-24 15:49:54,152: 
2023-11-24 15:49:54,152:   File "/home/SpaceMeerkat/4211-IOMS/thredspace/__init__.py", line 16, in <module>
2023-11-24 15:49:54,152:     import pandas as pd

The virtualenv is being correctly pointed at, so the packages should be found but for some reason pandas isn't.

I checked the compatibility of Werkzeug against that of Flask, as it's known issue that you tend to have to match them.

I even rolled them back to Werkzeug==2.3.7 and Flask==2.1.3 just to be sure.

Am I doing something monumentally stupid? Because from what I can tell the error logs are saying it's using the right virtualenv but the fact they're not found is really strange. I do wonder if trying to run an __init__ file to instantiate the app is a bad idea and could be an issue.


Solution

  • Please note that this has been solved. The WSGI script has a strict requirement on having the app be a variable named "application".

    See this forum post.