WSGI Error when Trying to Deploy Web Flask App with PythonAnyWhere

Viewed 416

I want to deploy my flask app publicly using pythonanywhere. I am new to this (I am a beginner). I have followed all steps exactly. But I have got some error on my web flask app. On my website, I use python3.7, HTML, CSS, and JavaScript. Here's the error message on error log:

2020-08-14 08:37:07,432: Error running WSGI application
2020-08-14 08:37:07,435: ModuleNotFoundError: No module named 'pandas'
2020-08-14 08:37:07,436:   File "/var/www/analisisbipolardisorder_pythonanywhere_com_wsgi.py", line 18, in <module>
2020-08-14 08:37:07,436:     from my_flask_app import app as application  # noqa
2020-08-14 08:37:07,436: 
2020-08-14 08:37:07,436:   File "/home/analisisbipolardisorder/mysite/my_flask_app.py", line 3, in <module>
2020-08-14 08:37:07,436:     from pandas import DataFrame, read_csv
2020-08-14 08:37:07,437:
***************************************************
2020-08-14 08:37:07,437: If you're seeing an import error and don't know why,
2020-08-14 08:37:07,438: we have a dedicated help page to help you debug: 
2020-08-14 08:37:07,438: https://help.pythonanywhere.com/pages/DebuggingImportError/
2020-08-14 08:37:07,439:
***************************************************

I really don't know how to fix that. On my file named 'my_flask_app.py', there are some warning like this:

  • 'flask.url_for' imported but unused (even though I need 'url_for' module in other file)
  • 'pandas' imported but unused (even though I need library pandas to import DataFrame and read_csv module)
  • 'pandas.DataFrame' imported but unused
  • 'pandas.read_csv' imported but unused

And also on my WSGI file, there is a warning like this:

  • 'my_flask_app.app as application' imported but unused (even though I need this to run my web flask app)

My Flask look like this (my_flask_app.py):

from flask import Flask, render_template, url_for
import pandas
from pandas import DataFrame, read_csv
import csv

app = Flask(__name__)

@app.route('/')
def index():
    with open (r"/home/analisisbipolardisorder/css/bipolarlabelled.csv", encoding="utf8") as file:
        reader = csv.reader(file)
        dataset = [row for row in reader]
    with open (r"/home/analisisbipolardisorder/css/df_train90.csv", encoding="utf8") as train_file:
        reader = csv.reader(train_file)
        train = [row for row in reader]
    with open (r"/home/analisisbipolardisorder/css/test_prediction_9010.csv", encoding="utf8") as test_file:
        reader = csv.reader(test_file)
        test = [row for row in reader]
    return render_template('home.html', data = dataset, train = train, test = test)

if __name__ == "__main__":
    app.run(debug=True)

And my WSGI file look like this:

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys

# add your project directory to the sys.path
project_home = '/home/analisisbipolardisorder/mysite'
if project_home not in sys.path:
    sys.path.insert(0, project_home)

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

I really need your kind of help. Thanks before.

2 Answers
  1. create an environment folder. It is simple. First: pip install virtualenv. At second create your env: python -m venv myenv. If you use a Windows machine then myenv\Scripts\activate.bat, source myenv/bin/activate it is for unix/macos. Then install all your libraries here. Then do like that pip freeze > requirements.txt. if you want to disable env deactivate.
  2. Initialize a Git

it was hard to write everything so watch this youtube video ("Deploying Flask Apps Using Python Anywhere")

As jen pointed out in the comments, you are missing pandas I would suggest you install a virtual environment in your project directory and then install pandas in that environment ([here is a pythonanywhere help post on that])1

If that seems too daunting, just open a bash console and type in python3.x -m pip install pandas the x in this code you would need to replace with the python3 version you have selected for your project (python3.8 or similar)

Let me know if you need any more information here

Related