Gunicorn Failed to Download Flask Application

I have a Flask application that I am trying to run through Gunicorn.

I am using virtualenv and python3. If I activate my venv cd in my application database directory, run:

gunicorn mysite:app 

I get:

 Starting gunicorn Listening at http://127.0.0.1:8000 DEBUG:mysite.settings:>>Config() ... Failed to find application: 'mysite' Worker exiting Shutting down: master Reason: App failed to load 

Search in / etc / nginx / sites -available I only have the file "default". In permitted sites I do not have a file.

In my nginx.conf file, I have:

 include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; 

Application structure:

 mysite #this is where I cd to and run gunicorn mysite:app --manage.py --/mysite ----settings.py ----__init__.py 

in manage.py for mysite I have the following:

 logger.debug("manage.py entry point") app = create_app(app_name) manager = Manager(app) if __name__ == "__main__": manager.run() 

In the __init__.py file:

 def create_app(object_name): app = Flask(__name__) #more setup here return app 

In my settings.py in the application directory

 class Config(object): logger.debug(">>Config()") #this logs OK so gunicorn is at least starting in correct directory 

From inside virtualenv if I run

 print(sys.path) 

I found a path to python and site-packages for this virtualenv.

From what I read to start the guns, it's just a matter of installing it and launching the mysite machine guns: app

Starting gunicorn from the parent directory of mysite I get the same thing that I did not find the application: "mysite", the application did not load the error, but does not receive the registration DEBUG ... Config () (since we are clearly in the wrong directory to start). Starting gunicorn from mysite / mysite (obviously wrong) I get an exception in the ereor workflow, ImportError: There is no module named "mysite".

Any tips on how I can fire guns?

+6
source share
1 answer

You point the shooting at mysite:app , which is equivalent to from mysite import app . However, there is no app object in the top ( __init__.py ) mysite import mysite . Tell the gunner to call the factory.

 gunicorn "mysite:create_app()" 

You can also pass arguments to the call.

 gunicorn "mysite:create_app('production')" 

Inside, this is equivalent to:

 from mysite import create_app app = create_app('production') 

Alternatively, you can use a separate file that performs the configuration. In your case, you have already initialized the app in manage.py .

 gunicorn manage:app 
+7
source

All Articles