Flask init-db no such command

Question:

From yesterday I read a lot of topics about the famous error:

"No such command init-db"

So, I followed the tutorial here: https://flask.palletsprojects.com/en/1.1.x/tutorial/database/

(I’m under Debian 9.6)

I do the following :

export FLASK_APP=webApp
export FLASK_ENV=development
flask init-db

I then tried:

python3 -m flask init-db

I also tried:

export FLASK_APP=webApp.py

But I still have the same error message.

Here is the tree of my project :

instance/
webApp/
├── auth.py
├── babel.cfg
├── dashboard.py
├── db.py
├── __init__.py
├── pdf.py
├── __pycache__
│   ├── auth.cpython-35.pyc
│   ├── db.cpython-35.pyc
│   ├── __init__.cpython-35.pyc
│   ├── pdf.cpython-35.pyc
│   └── ws.cpython-35.pyc
├── schema.sql
├── static

I didn’t see what I’ve missed.

Asked By: Nathan Cheval

||

Answers:

I found the problem.
When I launched flask --help, I saw there was an import error of a module. I just installed that module via pip, and it was okay.

Answered By: Nathan Cheval

Maybe you could simply try using
flask init_db

Note :- USE Underscore _ instead of hyphen -

Cross verify it by checking if a sqlite file is created in instance folder

It works like in the image

Answered By: Neha Kulkarni

I just had the same problem even though it worked the last time.
I just ran the flask, then stopped it and tried to initialize the database and it worked.

Answered By: Katarina Prskalo

In order to run init-db command, you first need to make sure you are outside the flaskr package (use pwd command if not sure) and at the same level as venv folder, then set the two below on the terminal:

$env:FLASK_APP = "flaskr"
$env:FLASK_ENV = "development"

Then run:

flask init-db

Output: Initialized the database.

You don’t need to install any extra packages if you follow the tutorial and execute the commands as they say.

Answered By: Felipe Alarcon

You must set FLASK_APP correctly. It must match the directory name where the app factory lives. In flaskr, this is in flaskr/__init__.py. The factory is named create_app().

So if you rename flaskr/, you must set FLASK_APP to match.

Ref: https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/

Answered By: sqqqrly

I got the same problem. After executing flask --help or just flask, I realised the problem was caused by an import error and there was a typo in my FLASK_APP.

Correcting it made everything work again.

Answered By: astrowq

First, be sure to activate your virtual environment.
If you are using a new terminal then run the command below:

export FLASK_APP="flaskr"
export FLASK_ENV=development

Run the flask app

flask run

After that run the DB command

flask init_db

You should see the Database Initialized message.

Answered By: Nurujjaman Nur

This worked for me:

flask init-db

I got this idea from the docs generated by the commands:

flask --help

screenshot of  output of above command

Answered By: user15163490

in my case the command I had to use was different, as seen from the flask –help output:

Commands:
  init.db
  routes   Show the routes for the app.
  run      Run a development server.
  shell    Run a shell in the app context.

So I had to use flask init.db for some reason.

Answered By: Heivers

Make sure you create and activate the virtual environment using the venv command from the command prompt.
Those are the windows cmd commands:

cd pathpyproject
pathmyproject> py -3 -m venv venv
pathmyproject> venvScriptsactivate
Answered By: Yoni Kremer

I had this issue, and the problem was I had not imported migrate in my app. I solved the issue by adding these two statements to my file:
from flask_migrate import Migrate and migrate = Migrate(app, db) .
I saved the file, then ran the flask db init command again, and it worked!

Answered By: Alvin

be careful that install flask_migrate in your env.
so in cmd:

  1. create your env :

cd myproject
py -3 -m venv venv

  1. activate your env:

venvScriptsactivate

  1. install flask_migrate:

pip install flask_migrate

Answered By: zhaleh

hope this will help somebody and maybe made it more clear.

In my case I run the following command:

flask --app flask_app_directory_name db init

Here I mentioned where my app is by the option "–app":
flask_app_directory_name – package of my app(or I think it could be a path to the file with the app).

Of course:

  • I have installed Flask-Migrate;
  • I have my flask app defined in init.py file of flask_app_directory_name package.
Answered By: burney
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.