question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to find "application" callable in file /app/webapp.py

See original GitHub issue

I am converting a flask application to Docker using uwsgi-nginx-flask-docker. I am using flask_restful to support a restful interface only.

Here are the relevant parts of my webapp.py (equivalent of main.py in the example):

#!flask/bin/python
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from flask import Flask, jsonify, make_response
from flask_restful import Api
from flask_cors import CORS
from actual_app.frontend import resources, useradmin, entityadmin
from flask_jwt_extended import JWTManager
import logging, logging.handlers

app = Flask(__name__, static_folder='static', static_url_path='')
app.config['CORS_HEADERS'] = 'Content-Type'

cors = CORS(app, resources={r"/api": {"origins": "http://localhost"}})

logging.basicConfig(level=logging.INFO)

app.debug = True
app.config['SECRET_KEY'] = 'app-is-cool'
app.config['JWT_ACCESS_LIFESPAN'] = {'hours': 24}
app.config['JWT_REFRESH_LIFESPAN'] = {'days': 30}

jwt = JWTManager(app)

api = Api(app)

api.add_resource(resources.Login, '/api/login', methods=['POST'])
api.add_resource(resources.Logout, '/api/logout', methods=['POST'])
api.add_resource(resources.RegisterOtp, '/api/register_otp', methods=['POST'])

...

@app.route('/', methods=['POST', 'GET'])
def index():
    return "<h1>Example Limited</h1>"

@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)

@app.errorhandler(500)
def server_error(e):
    logging.exception('An error occurred during a request. %s', e)
    return "An internal error occurred", 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=80, use_reloader=False)

I run it as docker run --name app_name -d -e URL=http://localhost -e EMAIL=Dev -e CLIENT=dev -e CACHE="1 Month" -v /datanew:/datanew -p 80:80 --rm myapp/web:v01

I have the Flask application named ‘app’ as required but get this error:

unable to find "application" callable in file /app/webapp.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. GAME OVER ***

Ideas on what might be wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, May 22, 2019

The problem is that the image is not being able to find your Flask app. The module in this case is webapp, by the error, it seems you seem to have that correctly set. But the callable is not application, but app. The callable is the variable with the Flask app.

Check the docs here: https://github.com/tiangolo/uwsgi-nginx-flask-docker#custom-uwsgiini-configurations

This is what I think your /app/uwsgi.ini file should look like:

[uwsgi]
module = webapp
callable = app

About migrating to FastAPI, I guess that code could look like:

@app.post("/register_otp/")
def register_OTP(user = Depends(get_current_user), email: str = Body(..., embed=True)):
    url = pyqrcode.create(user.get_totp_uri(users_file))
    users_cache.update(email, user, users_file)
    stream = BytesIO()
    url.svg(stream, scale=5)
    return {'register_otp': base64.b64encode(stream.getvalue()).decode()}

That would expect a JSON like:

{"email": "someemail@example.com"}

And the dependency get_current_user would be re-usable and would extract the JWT as described here: https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/

If you want to separate all the HTTP methods for /register-otp/, you could put them in a single Python file (a Python module), with a “router”, and then include them using the prefix URL /register_otp.

In that case, that would look like:

from fastapi import FastAPI, Body, Depends


@app.post("/")
def register_OTP(user = Depends(get_current_user), email: str = Body(..., embed=True)):
    url = pyqrcode.create(user.get_totp_uri(users_file))
    users_cache.update(email, user, users_file)
    stream = BytesIO()
    url.svg(stream, scale=5)
    return {'register_otp': base64.b64encode(stream.getvalue()).decode()}

Notice the @app.post("/") without /register_otp/.

And your docs would look like:

Fast API - Swagger UI - Google Chrome_053

0reactions
tiangolocommented, May 23, 2019

Great! Thanks for reporting back and closing the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

uWSGI cannot find "application" using Flask and Virtualenv
unable to find "application" callable in file /var/www/coefficient/flask.py. is the key :) Your app is defining an 'app' callable, ...
Read more >
python - Flask and uWSGI - unable to load app 0 (mountpoint ...
I had problems with the accepted solution because my flask app was in a variable called app . You can solve that with...
Read more >
Django+uWsgi unable to find "application" callable
The error is telling you that it can't find that variable name. And yet, you clearly are defining it.
Read more >
Bug listing with status CONFIRMED as at 2022/12/20 18:46:38
config file verification" status:CONFIRMED resolution: severity: ... Bug:75028 - "app-editors/xemacs-21.4.21-r1 fails to build: Segmentation fault" ...
Read more >
Newbie, first script isn't working - Remotify
File "/Applications/Ableton Live 11 ... Remote Scripts/test/__init__.py", line 4, in create_instance ... 'module' object is not callable
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found