Forums

WSGI application TypeError: 'module' object is not callable

Hi peeps,

I'm a beginner at coding in Python. I've just followed this tutorial to the letter: https://alysivji.github.io/flask-part2-building-a-flask-web-application.html. The app is working perfectly on my local machine. I am now trying to publish the app using pythonanywhere. The problem I have is I am unable to provision the WSGI file correctly to get the app to run?

The application is structured as follows:

/home/res/MyApp/

├── app.py
├── models.py
├── static
│   ├── css
│   │   ├── main.css
│   │   ├── normalize.css
│   │   └── normalize.min.css
│   └── js
│       ├── main.js
│       └── vendor
│           ├── jquery-1.11.2.min.js
│           └── modernizr-2.8.3-respond-1.4.2.min.js
├── status
└── templates
    ├── 404.html
    ├── all-dates.html
    ├── all-subreddits.html
    ├── base.html
    ├── by-subreddit.html
    └── index.html

On my local machine I simply type $ python app.py. and the application runs locally.

I have tried different variations on the WSGI file but i'm not even sure if I should be pointing it to the app.py file or if I need to restructure the application?

Here is the current WSGI as it stands (i've kept all other files the same):

    # /var/www/res_pythonanywhere_com_wsgi.py
    import sys

    path = '/res'
    if path not in sys.path:
        sys.path.insert(0,"/MyApp/app.py")

    sys.path.append('/home/res/')
    from MyApp import app as application

Any suggestions ?

The path you're adding to sys.path does not exist. From your directory tree, it should be /home/res/MyApp/. Then your import should be from app import something as application. Where something is the variable name of your flask app in app.py.

I made the changes as suggested, i think the variable name of my flask app is simply app? now getting the following error:

'app.app as application' imported but unused.

My app.py file is as follows, maybe there's something in there that needs changing?

from flask import Flask

def create_app(config):
    # create app and load config
    app = Flask(__name__)
    app.config.from_object(config)

    # initalize app with database
    from model import db
    db.init_app(app)

    # bind views
    from view import index, all_dates, by_date, all_subreddits, by_subreddit
    app.add_url_rule('/', view_func=index)
    app.add_url_rule('/timestamp_ms', view_func=all_dates)
    app.add_url_rule("/timestamp_ms/<day_to_pull>", view_func=by_date)
    app.add_url_rule('/lang', view_func=all_subreddits)
    app.add_url_rule('/lang/<sub_to_pull>', view_func=by_subreddit)

No. That's fine. It's just the syntax highlighter giving you a warning that you can ignore.

Excellent, thanks Glenn, much appreciated.