简体   繁体   中英

Problems running Flask Web App on linux Debian using Gunicorn and Nginx

So I'm currently trying to put my Web App online using Flask, Debian 9, Gunicorn. The problem I'm having, after typing > gunicorn wsgi:app:

[2019-11-15 12:22:11 +0000] [11643] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/root/testing/venv/local/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/root/testing/wsgi.py", line 1, in <module>
    from __init__ import app
  File "/root/testing/__init__.py", line 44, in <module>
    create_app()
  File "/root/testing/__init__.py", line 24, in create_app
    from .models import User
ValueError: Attempted relative import in non-package
[2019-11-15 12:22:11 +0000] [11643] [INFO] Worker exiting (pid: 11643)
[2019-11-15 12:22:11 +0000] [11639] [INFO] Shutting down: Master
[2019-11-15 12:22:11 +0000] [11639] [INFO] Reason: Worker failed to boot.

My wsgi.py:

from __init__ import app

if __name__ == "__main__":
    app.run()

My init .py:

# init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager 
from flask_bootstrap import Bootstrap

db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    Bootstrap(app)

    app.config['SECRET_KEY'] = 'secretkey'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from .models import User

    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    if __name__ == '__name__':
        app.run()

    return app

create_app()

My models.py:

models.py

from flask_login import UserMixin
from . import db

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(100))
    name = db.Column(db.String(1000))

Note: All indepencies are installed in my virtualenv, I've tried using gunicorn with only this code:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return "Hello World!"
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')

And that worked perfectly fine.

I changed every import that had a. and changed the init to app , deleted the create_app function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM