flask command Error: No such command ["create_db"]
Asked Answered
C

1

6

I want to run some commands from terminal with 'flask' command but it isn't working.

The Following is my project structure-

FlaskUserAuthentication
├── FlaskUserAuthentication
│   ├── API
│   │   ├── __init__.py
│   │   ├── db_models.py
│   │   └── routes.py
│   ├── Site
│   │   ├── __init__.py
│   │   ├── routes.py
│   │   ├── static
│   │   │   └── form_style.css
│   │   └── templates
│   │       └── Site
│   │           ├── base_layout.html
│   │           ├── index.html
│   │           ├── logout.html
│   │           ├── profile.html
│   │           ├── signin.html
│   │           └── signup.html
│   ├── __init__.py
│   └── commands.py
├── run.py
└── venv

As the flask run command runs the app, I am sure that my environment variable is set properly. However, when I try to use flask-cli-command like-

flask create_db

I get, Error: No such command "create_db".

The following is my FlaskUserAuthentication/commands.py file-

from FlaskUserAuthentication import app, db
from FlaskUserAuthentication.API.db_models import Group, Member, Project, Milestone

@app.cli.command('create_db')
def createDatabase():
    db.create_all()
    print('***** Datebase created ****')

#....some more commands

and the FlaskUserAuthentication/__init__.py module (where the Flask app instance is initiated)-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SECRET_KEY'] = 'justasamplekey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site

app.register_blueprint(api)
app.register_blueprint(site)
Castrate answered 25/11, 2019 at 12:35 Comment(4)
Check it - flask.palletsprojects.com/en/1.1.x/cli/…Laris
I don't know why my question had been downvoted but this is not a duplicate question as I tried other solutions on stack and addressed that too. Anyways, I need help, so downvote or not, can anyone please direct me to the right direction as I am confused on what I have done wrong.Castrate
try the following flask --help It shows you all the commands registered under flask cli. Moreover, your CLIGroup should be executed in the run.py where I assume you are running the server.Etui
flask --help didn't do much for me as it was only telling me how to execute a command and the three commands that was available but I needed to know the source of my issue.Castrate
C
4

The issue was that the command.py module was not recognized by the app. The solution, I tried is to initiate a blueprint in the commands module and then in the __init__.py module where I initiated the app instance, I registered the corresponding Blueprint with it. So, the updated commands.py-

from FlaskUserAuthentication import db
from flask import Blueprint
from FlaskUserAuthentication.API.db_models import Group, Member, Project, Milestone

@cmd = Blueprint('db', __name__) #created a Blueprint for this module

@cmd.cli.command('create_db') #rather than generating the cli command with app,
def createDatabase():         # used the blueprint, cmd
    db.create_all()
    print('***** Datebase created ****')

and the updated __init__.py module,

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SECRET_KEY'] = 'justasamplekey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site
from FlaskUserAuthentication.commands import cmd

app.register_blueprint(api)
app.register_blueprint(site)
app.register_blueprint(cmd) # registered the command module as blueprint

Now set the environment variable and development server with (in the terminal) -

export FLASK_APP=FlaskUserAuthentication/__init__.py
export FLASK_DEBUG=1

After that use the flask command like-

flask db create_db

P.S.: now because of the Blueprint rather than flask create_db, the command will be

flask db create_db

I needed to realise the root of the issue and for that I thank the author of the post-

Where should I implement flask custom commands (cli)

and the author, @emont01 for his response to the post.

Castrate answered 30/11, 2019 at 15:6 Comment(1)
thanks for the mentioned 'Where should I implement flask custom commands (cli)' post, that was helpful!Diplostemonous

© 2022 - 2024 — McMap. All rights reserved.