17

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.

3
  • 1
    Please reveal a db.py content. Commented Oct 15, 2019 at 7:54
  • Also, do you somewhere import db.py file? You need to do that in order to register command via Click. Commented Oct 15, 2019 at 7:57
  • 1
    It is done via from . import db in that tutorial you used. Commented Oct 15, 2019 at 8:02

19 Answers 19

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what to install?
to me it was that I forgot to activate the virtualenv before running the command
Maybe you have to install Flask Migrate using pip first. Using pip command : "pip install flask-migrate"
11

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!

1 Comment

These things should be mentioned clearly in docs. Very bad documentation. You saved my time
3

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.

Comments

3

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.

Comments

3

I was facing the same problem !!! I did this and my problem got solved :

1.Create and activate virtual env for your project.

2.There seems to be issues with latest Flask-Migrate. So use this command pip install --force-reinstall -v "Flask-Migrate==3.1.0"

3.Problem solved run flask db init

1 Comment

Did you find any solution for the newer version of Flask-Migrate? @arinjay-bhosale
2

i think the answer lies in what you export

 export FLASK_APP=main( name of your python file where you are running the app)
export FLASK_ENV=development

flask db migrate -m "Your migration message"

also don't keep no using init again and again. ideally have the init command in your main application file flask db upgrade

Comments

1

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.

Comments

1

Now maybe the command have changed but in case anyone still cant make it work with the command in the original question I had to use:

flask db init

this is for

flask version 3.0.3

Comments

0

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.

Comments

0

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.

Comments

0

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/

Comments

0

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

1 Comment

How is this different from existing answers? SO platform's strength relies on voting on existing answers, rather submitting duplicates. Please edit if you have some unique, insightful, new content to add to your post. Duplicate "answers" run the risk of down votes, or even removal.
0

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

Comments

0

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:

venv\Scripts\activate

  1. install flask_migrate:

pip install flask_migrate

Comments

0

I had the error message. Here were what I did:

  1. remove 'migrations' folder if exists;
  2. remove 'mydb.db'. (replace 'mydb.db' with yours)
  3. run flask --app flask_app_directory_name db init

Comments

0

So I had to specify the actual app name $FLASK_APP=mypythonflaskapp.py db migrate -m

Comments

0

After executing flask --help, I didn't find db-init command.enter image description here

Comments

-1

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

Comments

-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.