Flask init-db no such command

Viewed 16124

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.

11 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.

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.

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.

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.

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.

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

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

cd path\pyproject
path\myproject> py -3 -m venv venv
path\myproject> venv\Scripts\activate

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!

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

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.

Related