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

12,490

Solution 1

In my case I had to do

python3 -m flask db init

Without mentioning python3 -m, it shows

zsh: command not found: flask

Solution 2

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.

Solution 3

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.

Share:
12,490

Related videos on Youtube

mcorrigal
Author by

mcorrigal

Updated on June 04, 2022

Comments

  • mcorrigal
    mcorrigal almost 2 years

    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.

  • mojo706
    mojo706 about 7 years
    You were correct. Running python run.py on mine solved the issue after I found a typo. Good looking out
  • EMC
    EMC over 5 years
    This helped me as well, the true error gets swallowed when running flask db init. In my case I had forgotten to export an environment variable.