Error in running uwsgi with Flask on Ubuntu - python

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?

Related

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

How to setup the startup command for a flask azure web app

I've created a simple flask web app and managed to publish it with CI/CD from azure devops to an azure web app. The pipelines are working except the startup of my app.
If i look at the log files in the webapp this comes up -
logfile default_docker.log:
2020-01-08T13:04:17.017575225Z Documentation: http://aka.ms/webapp-linux
2020-01-08T13:04:17.017579025Z Python 3.7.5
2020-01-08T13:04:17.017582725Z Note: Any data outside '/home' is not persisted
2020-01-08T13:04:17.093756525Z Starting OpenBSD Secure Shell server: sshd.
2020-01-08T13:04:17.109540649Z Site's appCommandLine: gunicorn --bind = 0.0.0.0 --timeout 600 app: application
2020-01-08T13:04:17.110379356Z Launching oryx with: -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -bindPort 8000 -userStartupCommand 'gunicorn --bind = 0.0.0.0 --timeout 600 app: application'
2020-01-08T13:04:17.114336587Z Oryx Version: 0.2.20191105.2, Commit: 67e159d71419415435cb5d10c05a0f0758ee8809, ReleaseTagName: 20191105.2
2020-01-08T13:04:17.116548105Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-01-08T13:04:17.118951024Z Build Operation ID: |DGaRVt5jG5c=.2a144509_
2020-01-08T13:04:17.554659456Z Writing output script to '/opt/startup/startup.sh'
2020-01-08T13:04:17.784203265Z Found virtual environment .tar.gz archive.
2020-01-08T13:04:17.784884970Z Removing existing virtual environment directory /antenv...
2020-01-08T13:04:17.788272497Z Extracting to directory /antenv...
2020-01-08T13:04:32.810295030Z Using packages from virtual environment antenv located at /antenv.
2020-01-08T13:04:32.817794689Z Updated PYTHONPATH to ':/antenv/lib/python3.7/site-packages'
2020-01-08T13:04:36.780635398Z usage: gunicorn [OPTIONS] [APP_MODULE]
2020-01-08T13:04:36.780670499Z gunicorn: error: unrecognized arguments: app: application
my simplefied app treeview looks like this -
test_app/
venv/
application/
templates/
__init__.py
routes.py
errors.py
models.py
forms.py
app.py
i've tried different startup commands in azure portal "general setting" but to no solution
gunicorn --bind=0.0.0.0 --timeout 600 app:application
EDIT : added app.py and init.py
app.py:
from application import app, db
from application.models import User, Post
#app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Post': Post, 'Classrooms' : Classrooms, 'ClassSession' : ClassSession, 'Teacher' : Teacher}
init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from logging.handlers import RotatingFileHandler
import os
from flask_bootstrap import Bootstrap
import logging
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
bootstrap = Bootstrap(app)
from application import routes, models, errors
if __name__ == '__main__':
# * --- DEBUG MODE: --- *
app.run(host='127.0.0.1', port=5000, debug=True)
can anybody point me a direction to where i can solve this stupid problem.
thx a lot!!
The gunicorn command isn't actually pointing to the WSGI app object. Try this instead:
gunicorn --bind=0.0.0.0 --timeout 600 application:app

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.

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

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

Gunicorn import error: url_for not found in flask, but native flask run works fine

I can run my flask app fine using flask's built-in web server command run(), but I'm receiving the following Import error while trying to run my app using gunicorn. I use the command gunicorn project.app:app from the mainfolder.
The error being thrown off from views.py is:
from app import db, url_for ImportError: cannot import name url_for
My app is organized as follows:
mainfolder/
Procfile
bin
project/
app.py
_config.py
views.py
run.py
run.py
from views import app
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
app.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.config.from_pyfile('_config.py')
db = SQLAlchemy(app)
app.config['SECRET_KEY'], report_errors=False)
from views import *
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
_config.py
import os
#All the app's keys
Procfile
#this works fine
web: python project/run.py
#this doesn't work
web: gunicorn project.app:app
python project/app.py runs the app fine so why does gunicorn project.app:app throw off a module ImportError?
Your import for url_for needs to come before app = Flask(__name__)
Your app is working on flask's server because url_for is imported prior to the
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
statement. Gunicorn is getting the application before you have imported url_for.

Categories