uWSGI + Flask + nginx + Digital Ocean: uwsgi[1359]: --- no python application found - python

uWSGI + Flask + nginx + Digital Ocean: uwsgi[1359]: --- no python application found
my files look like this:
/projectfolder
/myapp
factory.py
config.py
__init__.py
wsgi.py
myapp.ini
factory.py:
def create_app():
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
return app
__init__.py:
from myapp.factory import create_app
app = create_app()
wsgi.py:
from myapp import app as application
if __name__ == '__main__':
application.run()
myapp.ini
[uwsgi]
module = wsgi:application
master = true
processes = 5
socket = myapp.sock
chmod-socket = 660
vacuum = true
die-on-term = true
env = APP_SETTINGS=myapp.config.Config
I tried running uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app and it seems to work, but using nginx and uwsgi, all I get is no python application found. All I can gather from this is that somehow, my import is broken in wsgi.py. I'm just not sure how.

Related

Heroku Flask App: Where to place wsgi.py?

Trying to follow best practices for a Flask app running in Heroku so I'm moving things from app.py to working with blueprints.
The current directory structure is as follows:
--root
--application
--admin_blueprint
--another_blueprint
--wsgi.py (app = create_app())
--__init__.py (this has def create_app, which handles creating my app)
--migration
--Procfile
--requirements.txt
--runtime.txt
--config.py
--manage.py
This is init.py
from flask import Flask
...
def create_app():
app = Flask(...)
...
return app
and this is wsgi.py
from application import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0')
I cannot for the life of me figure out how to do the Procfile correctly, this is what I previously had when I had app.py and wsgi.py in my root directory and it was working fine on Heroku:
web: gunicorn app:wsgi
I've tried some of these:
web: gunicorn application:wsgi
web: gunicorn application.wsgi
web: gunicorn --pythonpath application application:wsgi
web: gunicorn application.wsgi.py
web: gunicorn "application.wsgi.py"
web: gunicorn "application/wsgi.py"
flask run works because I've exported FLASK_APP=application.wsgi.py
Thank you.
Use application.wsgi:app
application.wsgi (the part before the colon) instructs gunicorn how to resolve the module.
and app (the part after the colon) gives the name of the WSGI application declared in the resolved module.
:app can be omitted and gunicorn defaults to looking in the module for a WSGI application with the name application.

Heroku: No Python Application Found for Flask App

Heroku log is showing: "--- no python application found, check your startup logs for errors ---"
My suspicion is the error within the module under uwsgi.ini file - I've tried multiple different permutations, but haven't been successful.
How can uwsgi.ini file be updated so that heroku can find the app?
Is the structure of my app not ideal for heroku deployment, if so how should I change the structure?
The Flask app has the structure below:
top_dir/
instance/
flask.cfg
sub_dir/
templates/
tests/
static/
users/
form.py
views.py
__init__.py
models.py
run.py
Procfile
uwsgi.ini
requirements.txt
App starts with: python run.py
#run.py
from sub_dir import app
if __name__ == "__main__":
app.run()
This is the code in sub_dir/init.py
from flask import Flask
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile('flask.cfg')
#config file in top_dir/instance
Here is the code in the uwsgi.ini file
[uwsgi]
http-socket = :$(PORT)
master = true
die-on-term = true
module = sub_dir:run #error here?
memory-report = true
Procfile
web: uwsgi uwsgi.ini

What are the differences in the "app" parameter of a Procfile? (I'm using a gunicorn server) [duplicate]

I use gunicorn --workers 3 wsgi to run my Flask app. If I change the variable application to myapp, Gunicorn gives the error AppImportError: Failed to find application: 'wsgi'. Why am I getting this error and how do I fix it?
myproject.py:
from flask import Flask
myapp = Flask(__name__)
#myapp.route("/")
def hello():
return 'Test!'
if __name__ == "__main__":
myapp.run(host='0.0.0.0')
wsgi.py:
from myproject import myapp
if __name__ == "__main__":
myapp.run()
Gunicorn (and most WSGI servers) defaults to looking for the callable named application in whatever module you point it at. Adding an alias from myproject import myapp as application or application = myapp will let Gunicorn discover the callable again.
However, the wsgi.py file or the alias aren't needed, Gunicorn can be pointed directly at the real module and callable.
gunicorn myproject:myapp --workers 16
# equivalent to "from myproject import myapp as application"
Gunicorn can also call an app factory, optionally with arguments, to get the application object. (This briefly did not work in Gunicorn 20, but was added back in 20.0.1.)
gunicorn 'myproject.app:create_app("production")' --workers 16
# equivalent to:
# from myproject.app import create_app
# application = create_app("production")
For WSGI servers that don't support calling a factory, or for other more complicated imports, a wsgi.py file is needed to do the setup.
from myproject.app import create_app
app = create_app("production")
gunicorn wsgi:app --workers 16
If you're trying to serve an app with variable name app within server/cats.py, you can start the server on port 8000 as follows:
gunicorn server.cats:app -b 0.0.0.0:8000

Having trouble running Flask app under uWSGI [duplicate]

This question already has answers here:
Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)
(3 answers)
Closed 6 years ago.
I am trying to run my Flask app under uWSGI and am getting:
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
The layout is:
/opt/myapp
/opt/myapp/wsgi.py
/opt/myapp/run.py
/opt/myapp/lib
/opt/myapp/app
/opt/myapp/app/blueprints.py
/opt/myapp/app/filters
/opt/myapp/app/filters/__init__.py
/opt/myapp/app/__init__.py
/opt/myapp/app/main.py
app/__init__.py contains the usual:
from flask import Flask
app = Flask(__name__)
app/main.py looks like:
import blueprints
import filters
from app import app
def run(debug, host='0.0.0.0'):
app.run(debug=debug, host=host)
wsgi.py looks like:
if __name__ == '__main__':
from app.main import app as application
application.run(host='0.0.0.0')
If I run python wsgi.py from the CLI, it works fine, the usual :5000 server.
If I run:
uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
I see the error, it cannot load the application.
uWSGI imports your wsgi.py. So this code is never executed:
if __name__ == '__main__':
from app.main import app as application
application.run(host='0.0.0.0')
You should create the application at module level:
from app.main import app as application
if __name__ == "__main__":
application.run(...)
You must of course leave the .run() method inside the main block, because you don't want uWSGI to execute that.
Have you tried something like ...
uwsgi -s 0.0.0.0:8080 --protocol=http --module myapp --callable app
I'm not 100% sure the --module and --callable options are correct, because I don't have your actual code in front of me, might be something like --module myapp.app --callable main or some other variant
From the documentation on using uwsgi together.
http://flask.pocoo.org/docs/0.10/deploying/uwsgi/
If it's in virtualenv You have to activate it by adding:
execfile(activate_this, dict(__file__=activate_this))
And I think You should define project directory:
import sys
sys.path.append('/opt/myapp/app')

Gunicorn can't find app when name changed from "application"

I use gunicorn --workers 3 wsgi to run my Flask app. If I change the variable application to myapp, Gunicorn gives the error AppImportError: Failed to find application: 'wsgi'. Why am I getting this error and how do I fix it?
myproject.py:
from flask import Flask
myapp = Flask(__name__)
#myapp.route("/")
def hello():
return 'Test!'
if __name__ == "__main__":
myapp.run(host='0.0.0.0')
wsgi.py:
from myproject import myapp
if __name__ == "__main__":
myapp.run()
Gunicorn (and most WSGI servers) defaults to looking for the callable named application in whatever module you point it at. Adding an alias from myproject import myapp as application or application = myapp will let Gunicorn discover the callable again.
However, the wsgi.py file or the alias aren't needed, Gunicorn can be pointed directly at the real module and callable.
gunicorn myproject:myapp --workers 16
# equivalent to "from myproject import myapp as application"
Gunicorn can also call an app factory, optionally with arguments, to get the application object. (This briefly did not work in Gunicorn 20, but was added back in 20.0.1.)
gunicorn 'myproject.app:create_app("production")' --workers 16
# equivalent to:
# from myproject.app import create_app
# application = create_app("production")
For WSGI servers that don't support calling a factory, or for other more complicated imports, a wsgi.py file is needed to do the setup.
from myproject.app import create_app
app = create_app("production")
gunicorn wsgi:app --workers 16
If you're trying to serve an app with variable name app within server/cats.py, you can start the server on port 8000 as follows:
gunicorn server.cats:app -b 0.0.0.0:8000

Categories

Resources