0

my flask app is a package named app located at /Users/gexinjie/Codes/MyProject/xinnjie_blog

the file tree is like this

xinnjie_blog
├── app
|   ├── __init__.py
│   ├── config.py
│   ├── exceptions.py
│   ├── model.py
│   ├── model_sqlalchemy.py
│   ├── static
│   ├── templates
│   ├── util.py
│   └── views
├── manage.py

I export it as PATHONPATH, so manage.py can import app

echo $PATHONPATH
/Users/gexinjie/Codes/MyProject/xinnjie_blog

and export FLASK_APP

echo $FLASK_APP
manage.py

current dir is /Users/gexinjie/Codes/MyProject/xinnjie_blog

pwd
/Users/gexinjie/Codes/MyProject/xinnjie_blog

here is the manage.py

import click
from app import create_app

app = create_app('development')

@app.cli.command()
def initdb():
    click.echo('Init the db...')

here is app.__init__.py

from flask import Flask
from .model_sqlalchemy import db

def create_app(config_name='default'):
    app = Flask(__name__)
    ...   # init
    return app

but then if I execute flask initdb, I get this error:

Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb".

and if I execute flask run, I get

Usage: flask run [OPTIONS]

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

why manage.py is not found? And how can I fix it. (actually it worked well when manage.py have flask app in itself )

# manage.py
# this work well
app = Flask(__name__)  # not app = create_app('development')

Thank you

xinnjie
  • 672
  • 5
  • 12
  • Is `app` a file or a directory? Can you post its full contents? – Nathan Wailes Jul 08 '17 at 07:50
  • @NathanWailes `app` is a directory. I have posted its full content. – xinnjie Jul 08 '17 at 14:16
  • Can you post the contents of your `app/__init__.py`? – Nathan Wailes Jul 08 '17 at 14:37
  • @NathanWailes I have posted `__init__.py` – xinnjie Jul 09 '17 at 02:40
  • Are you using Linux as your OS or Windows? – Nathan Wailes Jul 09 '17 at 06:35
  • @NathanWailes these codes run on mac os. I think it has some problem during importing .because when manage.py initializes a Flask instance by itself, the script works well.However, there is so little error informattion given by flask command tool. – xinnjie Jul 09 '17 at 11:23
  • what does `flask --help` get you? Also I am assuming you have `app.run()` at the bottom of manage.py under the `if __name__ == '__main__':` – Adam Jul 09 '17 at 14:52
  • Also I would try doing the absolute path so `export FLASK_APP=$(pwd)/manage.py` – Adam Jul 09 '17 at 14:57
  • @Adam add `app.run()` after `if __name__ == '__main__'` does not help, it makes the script run the flask app directly. But I tried adding `app.cli() `, then it works partially after I executed `python manage.py `(`run` does't exist). `flask` command still can't find `manage.py`.(I tried absolute path) – xinnjie Jul 09 '17 at 15:59
  • I posted an answer but realized it wasn't correct when tried it on an app of mine. Here is my [code](https://hastebin.com/quyafecaqa.py) and it works. I honestly don't know whats up with yours. Are you using any type of virtual environment? – Adam Jul 09 '17 at 16:24
  • @Adam Thanks to your advice! That remind me to check flask version. I test `manage.py` all the time on Pycharm command tool, and in that tool `flask` is installed by Anaconda's python 3.6. I usually use python3.5 on terminal. So `flask` may lack some extensions and cause the problem.(though I'm not sure).after I uninstalled Anaconda, every thing goes well now. – xinnjie Jul 10 '17 at 04:29
  • @Adam by the way, the error message given by `flask` command tool is so confusing. – xinnjie Jul 10 '17 at 04:30

1 Answers1

0

Thank to @Adam, This problem was solved after I uninstall Anaconda.

Because all the time I tested manage.py on Pycharm command tool, and that flask was installed by Anaconda(python version 3.6), it may lack some extensions(usually I use python3.5 on terminal).So I think the problem occur during importing.

The flask command tool complain about 'can't locate the app', but the real problem is import error.So that is so confusing.


"universal" solution:

So when if you come to this kind of problem like I do, I suggest you first check the location of your app(try both relative path and absolute path), relative path may cause the locate problem when you are not at the right working directory.So absolute path is recommend.

If every thing about path went well, then make sure that all the packages required by your app is installed and can be imported. if you are using some type of virtual environment or something like that (In my case, I use the other version of python lacking some flask extensions), it may be the import error makes flask complain.

Hope this can help you.

xinnjie
  • 672
  • 5
  • 12
  • 1
    If you are using PyCharm I would create a virtualenv for every project and select the system installed interpreter as your base and then use the terminal inside PyCharm to do any commands. – Adam Jul 10 '17 at 17:54