Flask/uWSGI throws error unable to load app 0 (mountpoint='') - python

Im trying to run (just for testing purposes):
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi_prod
from a dir with:
home.py
home.pyc
wsgi_prod.py
wsgi_prod.pyc
my wsgi_prod.py has:
from home import app
if __name__ == "__main__":
app.run()
home.py has:
from flask import Flask, render_template
from jinja2 import Template
app = Flask(__name__, static_folder='static')
#app.route('/')
def home():
print('Hello world')
if __name__ == '__main__':
app.run(host='0.0.0.0')
When I run it, it throws:
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only)
What am I doing wrong? Why cant it see my app?
Thank you!

Well, I hope this helps someone out someday. Digital Ocean offers a great article to setup your flask/uwsgi, but offers the wrong command for this.
Use this instead:
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi_prod:app
and use this link

Related

gunicorn: Failed to find attribute 'app' in module

Hi I am trying to run,
gunicorn --bind localhost:8000 --worker-class sanic_gunicorn.Worker module:app
where I have following files
# ls
build
setup.py
dist
module
module.egg-info
venv
#cd module
#ls
__init__.py
__pycache__
__main__.py
app.py
content of __main__.py is as follows
from module.app import create_app_instance
if __name__ == '__main__':
app = create_app_instance()
app.run()
and content of app.py is
#some imports
def create_app_instance():
app = Sanic(name = "app_name")
.....
return app
I am using Sanic web framework and when I am running it's dev server as python -m module it works fine
python3 -m module
[2021-06-16 22:31:36 -0700] [80176] [INFO] Goin' Fast # http://127.0.0.1:8000
[2021-06-16 22:31:36 -0700] [80176] [INFO] Starting worker [80176]
can someone let me know what am I doing wrong ?
The simple answer is that there's no app exposed inside the module. You have the create_app_instance() method but this is not called.
I would suggest for you to refactor your code as follows. File structure would be:
./wsgi.py
./module/__init__.py
And the contents of those files as below:
.\wsgi.py
from module import create_app_instance
app = create_app_instance()
if __name__ == '__main__':
app.run()
.\module\__init__.py
# this is the contents of your current app.py
#some imports
def create_app_instance():
app = Sanic(name = "app_name")
.....
return app
and then the gunicorn line to start the server would be (please note the comment from The Brewmaster below):
gunicorn --bind localhost:8000 --worker-class sanic_gunicorn.Worker wsgi:app
What this does is it calls the exposed app instance inside wsgi.py. The __main__.py is not needed, and the code from your app.py has been moved to the __init__.py
I highly advise you to read through documentation/tutorials for Application Factory Pattern for Flask. The principle itself is the same as for Sanic, but there's more articles that describe the principle for Flask...

How to deploy Python API on heroku ? No module named 'app' error

I'm a total beginner with heroku and API's but I needed to execute some python for a mobile app project.
Anyway I watched a ton of tutorial and looked for the doc on heroku website but my problem remains the same :
I push my project using $ git push heroku master but when loading the page where my API should appear I get an application error from heroku which tells to check the logs.
So in the logs I am facing the following error : << ModuleNotFoundError: No module named 'app' >>
I have this code :
app.py
import flask
app = flask.Flask(__name__)
#app.route('/')
def home():
return 'hello'
Procfile
web: gunicorn testdamien.wsgi
wsgi.py
from app import app
if __name__ == '__main__':
app.run()
I don't really know what's the use of the wsgi file, I saw some youtubers not using it and directly putting the app.run() in the app.py.
Also, I tried with the following Procfile :
web: gunicorn app:app
don't work either...
Thank you for reading me.
So in your app.py your application is not running. Edit the file as below:
import flask
app = flask.Flask(__name__)
#app.route('/')
def home():
return 'hello'
if __name__ == '__main__':
app.run(host='0.0.0.0')
in your Procfile you are specifying your entry-point the format is web: gunicorn your_module_name:your_application name In your case it will be:
web: gunicorn app:app
Should work fine

Error in running uwsgi with Flask on Ubuntu

I have trouble setting up my project on DigitalOcean. i'm new on Python.
Currently using flask to create a simple app.
My directory in the ubuntu server is as such:
~/kaboong/{{all files}}
the main py file is called main.py:
from flask import Flask, render_template, request, redirect,jsonify, url_for, flash
app = Flask(__name__)
if __name__ == '__main__':
app.debug = True
app.run(host = '0.0.0.0', port = 8000)
Inside my wsgi.py file:
from main import app
if __name__ == '__main__':
app.run()
I tried running the wsgi as such :
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
I got an error saying:
* no app loaded. going in full dynamic mode *
Whats the problem here?
can you try this?
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi:app --callable app
if does not works, can you show more info such log console full?

Flask __init__.py doesn't show changes

When I try to update the __init__.py file in Flask, it doesn't show the changes in the server, but when I edit home.html it works fine.
app/__init__.py
import os
from flask import Flask, render_template
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
app.wsgi_app = ProxyFix(app.wsgi_app)
app.debug = bool(os.environ.get('PRODUCTION'))
if __name__ == '__main__':
app.run()
Any tips?
We solved the problem in comments but I will add solution here if someone else has a similar problem.
For development environment add debug=True argument to your app
app.run(debug=True)
If your development environment works on an application server, then you should look for autoreload option. In uWSGI there is py-auto-reload for example.
For released, stable environment you should restart your application server.
For example in uWSGI
There are several ways to make uWSGI gracefully restart.
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`
# or the convenience option --reload
uwsgi --reload /tmp/project-master.pid
# or if uwsgi was started with touch-reload=/tmp/somefile
touch /tmp/somefile
More: http://uwsgi-docs.readthedocs.io/en/latest/Management.html#reloading-the-server
Warning: if you combine application and web server, uWSGI and Nginx for example, then restarting Nginx won't reload your application code. Focus on the application server.

Auto reloading Flask app when source code changes

I know for a fact that Flask, in debug mode, will detect changes to .py source code files and will reload them when new requests come in.
I used to see this in my app all the time. Change a little text in an #app.route decoration section in my views.py file, and I could see the changes in the browser upon refresh.
But all of a sudden (can't remember what changed), this doesn't seem to work anymore.
Q: Where am I going wrong?
I am running on a OSX 10.9 system with a VENV setup using Python 2.7. I use foreman start in my project root to start it up.
App structure is like this:
[Project Root]
+-[app]
| +-__init__.py
| +- views.py
| +- ...some other files...
+-[venv]
+- config.py
+- Procfile
+- run.py
The files look like this:
# Procfile
web: gunicorn --log-level=DEBUG run:app
# config.py
contains some app specific configuration information.
# run.py
from app import app
if __name__ == "__main__":
app.run(debug = True, port = 5000)
# __init__.py
from flask import Flask
from flask.ext.login import LoginManager
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail
import os
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
#mail sending
mail = Mail(app)
lm = LoginManager()
lm.init_app(app)
lm.session_protection = "strong"
from app import views, models
# app/views.py
#app.route('/start-scep')
def start_scep():
startMessage = '''\
<html>
<header>
<style>
body { margin:40px 40px;font-family:Helvetica;}
h1 { font-size:40px; }
p { font-size:30px; }
a { text-decoration:none; }
</style>
</header>
<p>Some text</p>
</body>
</html>\
'''
response = make_response(startMessage)
response.headers['Content-Type'] = "text/html"
print response.headers
return response
The issue here, as stated in other answers, is that it looks like you moved from python run.py to foreman start, or you changed your Procfile from
# Procfile
web: python run.py
to
# Procfile
web: gunicorn --log-level=DEBUG run:app
When you run foreman start, it simply runs the commands that you've specified in the Procfile. (I'm going to guess you're working with Heroku, but even if not, this is nice because it will mimic what's going to run on your server/Heroku dyno/whatever.)
So now, when you run gunicorn --log-level=DEBUG run:app (via foreman start) you are now running your application with gunicorn rather than the built in webserver that comes with Flask. The run:app argument tells gunicorn to look in run.py for a Flask instance named app, import it, and run it. This is where it get's fun: since the run.py is being imported, __name__ == '__main__' is False (see more on that here), and so app.run(debug = True, port = 5000) is never called.
This is what you want (at least in a setting that's available publicly) because the webserver that's built into Flask that's used when app.run() is called has some pretty serious security vulnerabilities. The --log-level=DEBUG may also be a bit deceiving since it uses the word "DEBUG" but it's only telling gunicorn which logging statements to print and which to ignore (check out the Python docs on logging.)
The solution is to run python run.py when running the app locally and working/debugging on it, and only run foreman start when you want to mimic a production environment. Also, since gunicorn only needs to import the app object, you could remove some ambiguity and change your Procfile to
# Procfile
web: gunicorn --log-level=DEBUG app:app
You could also look into Flask Script which has a built in command python manage.py runserver that runs the built in Flask webserver in debug mode.
The solution was to stop using foreman start as stated in the comments and directly execute python run.py.
This way, the app.run method with the debug=True and use_reloader=True configuration parameters take effect.
Sample Application where app is our application and this application had been saved in the file start.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hallo():
return 'Hello World, this is really cool... that rocks... LOL'
now we start the application from the shell with the flag --reload
gunicorn -w 1 -b 127.0.0.1:3032 start:app --reload
and gunicorn reloads the application at the moment the file has changed automaticly. No need to change anything at all.
if you'd love to run this application in the background add the flag -D
gunicorn -D -w 1 -b 127.0.0.1:3032 start:app --reload
-D Demon mode
-w number of workers
-b address and port
start (start.py) :app - application
--reload gunicorns file monitoring
Look at the settings file:
http://docs.gunicorn.org/en/latest/settings.html
all options and flags are mentioned there. Have fun!

Categories