-1

Hello all and Happy Holidays,

I am trying to serve a flask application via wsgi and nginx, because I read it's a much more safer way. I am currently following this guide -> https://www.rosehosting.com/blog/how-to-deploy-flask-application-with-nginx-and-gunicorn-on-ubuntu-20-04/

I have Ubuntu 22.04. The problem is that when i execute

FLASK_ENV=developmnent FLASK_APP=server.py flask run

everything works fine and I can access the hostname with no problems. According the guide, to assure that everything works perfectly, I need to execute the server.py script via python3 command too. When I do that I get the following error:

File "../server/venv/lib/python3.10/site-packages/flask_restplus/api.py", line 215, in __getattr__
    return getattr(self.default_namespace, name)
AttributeError: 'Namespace' object has no attribute 'run'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "../server/server.py", line 317, in <module>
    app.run(host='0.0.0.0')
  File "../server/venv/lib/python3.10/site-packages/flask_restplus/api.py", line 217, in __getattr__
    raise AttributeError('Api does not have {0} attribute'.format(name))
AttributeError: Api does not have run attribute

Below you can view my server.py:

### IMPORTS ###

### GLOBAL VARIABLES ###

### FLASK CONFIGURATION ###
flask_app = Flask(__name__)
flask_app.secret_key = '...'

authorizations = { # AAI
    'apikey': {
        'type': 'apiKey',
        'in': 'header',
        'name': 'X-API-KEY'
    }
}

app = Api(app = flask_app, authorizations=authorizations,
          version = "1.1",
          title = "Title",
          description = "desc"
api = app.namespace('psa-controller', description='desc')
### FLASK CONFIGURATION ###

### MODEL ###

### UPLOAD PARSER ###
upload_parser = api.parser()
upload_parser.add_argument('file', location='files', required=True, type=FileStorage, help="Acceptable extensions: ['png', 'jpg', 'jpeg']")
### UPLOAD PARSER ###

### METHODS ###

### CONTROLLERS ###

### RUN ###
if __name__ == '__main__':
    app.run(host='0.0.0.0')
### RUN ###
#%%

Not sure why the python execution gets me an error while the flask doesn't (or surpresses it).

I've finished the guide successfully with the demo flaskapp.py, so I guess I need to get over my errors to continue first. Any help would be awesome!

Thank you

  • Please reduce your code to a [mcve] (we don't need to see all those model-related functions) when error happens on `app.namespace()` line – OneCricketeer Dec 23 '22 at 21:32
  • Also, linked page tells you to use `gunicorn` to actually run the code, so why are you trying to use `python3` directly? – OneCricketeer Dec 23 '22 at 21:35
  • Ok I deleted everything not related. I follow the steps of the linked page one by one and when executing `python3 server.py` the error occurs. At first I neglected it but later on I cannot proceed to the other steps (same error appears when I execute `gunicorn --bind 0.0.0.0:5000 wsgi:app`) – user2010686 Dec 23 '22 at 21:45
  • I think you want to use `flask_restx`, not `flask_restplus` - https://github.com/noirbizarre/flask-restplus/issues/777#issuecomment-1146446193 Also see README - https://github.com/noirbizarre/flask-restplus#flask-restplus – OneCricketeer Dec 23 '22 at 23:02

1 Answers1

0
File "../server/server.py", line 317, in <module>
    app.run(host='0.0.0.0')

You're using the wrong app

This should say flask_app.run(host='0.0.0.0')

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245