-1

This is not the same as "What is the purpose of the -m switch" as I'm talking about a function that runs independently (flask) and wanting to know why it works differently with -m. The answers there do not resolve the question.

I have a flask app that I'm building. It's set up with a subdirectory for the code, so

setup.py
gallery/
   __init.py__
   models.py
   view.py

When I run it using 'python -m flask run' it works. When I run it just as 'flask run' it doesn't. I haven't been able to find anyone who knows what the actual difference is between the two commands, so I'm kind of stuck.

Here's the output from 'flask run':

(venv)MacBook-Pro-4:Finished khunter$ export FLASK_APP=gallery
(venv)MacBook-Pro-4:Finished khunter$ flask run
Usage: flask run [OPTIONS]

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

Any thoughts about where the difference lies so I can use 'flask run' appropriately?

EDIT: It looks like I can get through this with pip install --editable to grab the setup.py I have there.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Kirsten Jones
  • 2,681
  • 1
  • 17
  • 20
  • This might interest you. [here](https://stackoverflow.com/questions/7610001/what-is-the-purpose-of-the-m-switch) and [here](https://stackoverflow.com/questions/46319694/what-does-it-mean-to-run-library-module-as-a-script-with-the-m-option) – Paritosh Singh Jan 05 '19 at 17:26
  • It's not a duplicate. Flask is supposed to run as its own independent function - 'flask run' should work exactly like 'python -m flask run' – Kirsten Jones Jan 05 '19 at 17:56
  • "just fails" means what? Throws an import error? Returns immediately with no output? Something else? Comparing the output of `python -c 'import flask; print(flask.__file__)'` and `command -v flask` at the shell may (or may not) be useful. Including the values of the environment variables `PATH` and `PYTHONPATH`, likewise. – Charles Duffy Jan 05 '19 at 18:01
  • It fails with "cannot find gallery". But gallery is set up fine, since it works with python -m. There's probably an unreported error (flask tends towards those) but I have no visibility into it because all of the examples say "Doesn't run? Do it with python -m to find out why!" - but it does not fail in the same way. – Kirsten Jones Jan 05 '19 at 18:03
  • That error is *relevant*; it tells us very explicitly that you have a different `sys.path` between the two invocation methods, which wasn't clear before. [Edit] its exact text into the question. – Charles Duffy Jan 05 '19 at 18:04
  • (actually, if your shell is bash, `type flask` is a step up from `command -v flash`, as it'll describe any aliases or shell functions involved in invocation). – Charles Duffy Jan 05 '19 at 18:07
  • ...indeed, please consider all the above (`type flask`, `type python`, `declare -p PATH PYTHONPATH`) requests to add details to the question, as opposed to merely advice on debugging steps you might run yourself. – Charles Duffy Jan 05 '19 at 18:14
  • ...and btw, the "if app is not on PYTHONPATH" advice in the error message is not a bad place to start. Does running `PYTHONPATH=.:$PYTHONPATH FLASK_APP=gallery flask run` change behavior? (When putting variable assignments at the front of a shell command they're both automatically exported and scoped only to that one command, so the intent is to run that as a single command). – Charles Duffy Jan 05 '19 at 18:15

1 Answers1

0

There isn't many differences. When you run python -m flask run, you are running flask/__main__.py.

While when you run flask run, you are running flask/cli.py.

As you can see, the first one is just an alias of the second one. Although it passes as_module == True. The reason why it need to be additionally processed is showed in comment. You can check it by yourself.

I believe your issue is caused by path problem. Double check if your path is correct.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • What path? And how can I check it? With 'flask run' I don't get any useful output about *anything* (it just fails) – Kirsten Jones Jan 05 '19 at 17:51
  • @KirstenJones Well, I think the comments of your question have clarified what I mean about path. – Sraw Jan 05 '19 at 19:25