简体   繁体   English

从源目录运行时,“flask run”找不到代码,而“python -m flask run”成功

[英]'flask run' can't find code when run from source directory, while 'python -m flask run' succeeds

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.这与“-m 开关的目的是什么”不同,因为我在谈论一个独立运行的函数(烧瓶)并想知道为什么它与 -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.当我使用 'python -m flask run' 运行它时,它可以工作。 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.编辑:看起来我可以通过 pip install --editable 获取我在那里的 setup.py 来解决这个问题。

There isn't many differences.没有太多区别。 When you run python -m flask run , you are running flask/__main__.py .当您运行python -m flask run ,您正在运行flask/__main__.py

While when you run flask run , you are running flask/cli.py .当您运行flask run ,您正在运行flask/cli.py

As you can see, the first one is just an alias of the second one.如您所见,第一个只是第二个的别名。 Although it passes as_module == True .虽然它通过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.仔细检查您的路径是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM