[Python] Flask Does Not Find Application In Raspberry Pi

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    Firstly I would like to say that I have the source code working with flask on MacOS and Windows 10 (various people are developing the site). We want to run our source code in the Raspberry Pi 3 B+. I have installed flask and python on RPI. Here are the details:

    flask --version
    Flask 0.12.1

    Python 3.5.3 (default, Sep 27 2018, 17:25:39)


    The only difference between my MacOS is that Python is 3.7.5 and Flask is 1.0.2. (I tried to update python and flask on RPI multiple times and it would always install the same)

    This is where the versions case negate, I made an arbitrary python file in RPI:

    from flask import Flask, render_template, request
    app = Flask(__name__)

    @app.route('/')
    def test():
    return "HELLO"

    if __name__ == '__main__':
    app.run(debug = True, port=8080)


    python3 test.py


    This runs the flask server on RPI's localhost no problem.

    The problem is when I try to run the source code on RPI via:

    export FLASK_APP=SmartLock
    export FLASK_ENV=development
    flask run


    I get the following message:

    pi@raspberrypi:~/Downloads/CSI4999 $ export FLASK_APP=SmartLock
    pi@raspberrypi:~/Downloads/CSI4999 $ flask run
    Usage: flask run [OPTIONS]

    Error: Failed to find application in module "SmartLock". Are you sure it contains a Flask application? Maybe you wrapped it in a WSGI middleware or you are using a factory function.


    My structure

    CSI4999
    |
    SmartLock
    |
    -static
    -templates
    __init__.py
    other modules


    I run the commands from inside CSI4999 (like I do on PC). Unless Raspberry Pi would require a different structure, I don't see why it runs on MacOS and Windows 10 and not on RPI.

    I searched over the Flask Documentation (I also tried [https://flask.palletsprojects.com/en/1.1.x/tutorial/factory/]) and internet nothing helped.

    I even tried to do:

    python -m flask run


    but that would return

    File "/usr/lib/python3.5/socketserver.py", line 454, in server_bind
    self.socket.bind(self.server_address)
    OSError: [Errno 98] Address already in use


    Any input would greatly appreciated.

    [Update:]

    I have installed Python 3.7 and Flask 1.1 flask PATH is :

    PATH:/usr/lib/python3/dist-packages/flask

    pi@raspberrypi:~ $ flask --version
    Python 3.7.5
    Flask 1.1.1
    Werkzeug 0.16.1


    Inside pi@raspberrypi:~/Downloads/CSI4999/

    FLASK_APP=SmartLock
    flask run


    returns:

    * Serving Flask app "SmartLock"
    * Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
    * Debug mode: off
    /usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
    'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
    Traceback (most recent call last):
    File "/usr/local/bin/flask", line 8, in <module>
    sys.exit(main())
    File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
    File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
    File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "/usr/local/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
    File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 860, in run_command
    extra_files=extra_files,
    File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 1012, in run_simple
    inner()
    File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 965, in inner
    fd=fd,
    File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 808, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
    File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 701, in __init__
    HTTPServer.__init__(self, server_address, handler)
    File "/usr/local/lib/python3.7/socketserver.py", line 452, in __init__
    self.server_bind()
    File "/usr/local/lib/python3.7/http/server.py", line 137, in server_bind
    socketserver.TCPServer.server_bind(self)
    File "/usr/local/lib/python3.7/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
    OSError: [Errno 98] Address already in use



    Which means flask runs and module is found. Now this is another issue. Any Ideas?

    [Update]

    REFLASHING RASPIAN ON SDCARD SOLVED ISSUE. USE ONLY PIP3 TO INSTALL LIBRARIES

    Continue reading...

Compartilhe esta Página