Flask-Migrate command 'flask db init' can't find app file

19.5k views Asked by At

Firstly, I'm following the Python Flask tutorial posted here: https://scotch.io/tutorials/build-a-crud-web-app-with-python-and-flask-part-one.

Everything was working smoothly up to the 'Migration' section where executing:

$ flask db init

... failed with the following error:

Usage: flask db init [OPTIONS]

Error: The file/path provided (run) does not appear to exist.  Please verify the path is correct.  If app is not on PYTHONPATH, ensure the extension is .py

I know the $FLASK_APP env variable is set because this command executes fine:

$ flask run

Can anyone suggest why this executes fine when running the app, but not when trying to create the migration repository?

The closest I can find elsewhere on the subject is here: Flask can't find app file, but pre-pending with python -m isn't working in either case for me here.

8

There are 8 answers

2
mcorrigal On

The command flask run started the app successfully.

The command flask db init failed with the error reported in the question.

So I tried python run.py and this failed to start the app by reporting an unmet dependency in models.py which was in fact a typo in an import. Fixing the typo and rerunning python run.py was successful.

Then I tried flask run again, still fine. Then flask db init... finally success.

It appears the error reported that it could not find run.py is either misleading or masking the true root cause of why it could execute.

0
Leandra McGriff On

This worked for me:

pip install flask-migrate --upgrade
0
Aslam On

I had same problem occuring,after sometime i noticed that the set FLASK_APP=my_app,but i had a function handling creaion and initializing app so i changed set FLASK_APP=my_app:crate_flask_app,finally it's working for me. for example:In my_app/init.py

migrate=Migrate()
db=Sqlalchemy()
defcreate_flask_app():
 app=Flask(__name__)
 db.init_app(app)
 migrate.init_app(app,db)
 return app

solution for windows is while declaring FLASK_APP environmental variable if any function is part of creating app then the function should be given after the colon i,e:set FLASK_APP:create_flask_app

0
danil51608 On

If you're using app factory then you should call init_app just as you call that on your db instance.

from flask_migrate import Migrate

db = SQLAlchemy()
migrate = Migrate()

def create_app():
    app = Flask(__name__)
    db.init_app(app)
    migrate.init_app(app, db)
    return app
0
Dishant Tanwar On

In my case my app name was different, and in the directory, I had two flask apps app.py and app_async.py And I was also getting the same migrate key error, so this is how I solved it:

FLASK_APP=app_async.py flask db init

Here I mentioned the app name, and then ran the command.

0
kaizen On

In my case I had to do

python3 -m flask db init

Without mentioning python3 -m, it shows

zsh: command not found: flask
0
user17838564 On

The problem is that you are not invoking the command properly. The syntax is, assuming you use a factory function in order to align with the factory pattern:

flask --app module_name:factory_fn_name db init. 

If you are not using the factory pattern, the thing simplifies to:

flask --app module_name db init
0
Junglemath On

I had the exact same issue, but upon closer inspection I had migrate = Migrate(app) instead of migrate = Migrate(app, db).