Flask CLI command not in __init__.py file Flask CLI command not in __init__.py file flask flask

Flask CLI command not in __init__.py file


Use click (the library behind Flask commands) to register your commands:

# commands.pyimport click@click.command()def test_run():    print('here')

In your __init__.py import and register the commands:

# __init__.pyfrom yourapp import commandsfrom flask import Flaskapp = Flask(__name__)app.cli.add_command(commands.test_run)

Take a look at the official docs


In you commands.py you can add register function like that:

def register(app):    @app.cli.command(name='test_run', help="test_run command")    def register_test_run():        test_run()    # other commands to register

And then in your __init__.py import this register function

from flask import Flaskapp = Flask(__name__)from commands import registerregister(app)

Notice, that you should import register after you've initialized your app.