在使用Flask教程在线构建一个小应用程序后,我不断收到这个ImportError:
eloiim:~ iivri.andre$ export FLASK_APP=Minimalapp.py
eloiim:~ iivri.andre$ flask run
Traceback (most recent call last):
File "/usr/local/bin/flask", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 478,in main
cli.main(args=args, prog_name=name)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 345, in main
return AppGroup.main(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 388, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 124, in __init__
self._load_unlocked()
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 148, in _load_unlocked
self._app = rv = self.loader()
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 209, in load_app
rv = locate_app(self.app_import_path)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 89, in locate_app
__import__(module)
ImportError: No module named Minimalapp.py
这是我的代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def IsSelf() :
return 'Hello 这是自'
这是我的ls -R输出
eloiim:learnFlasC iivri.andre$ ls
Hello.py Minimalapp.py
Http.py resource_locator.py
我不确定我做错了什么。有什么问题,我该如何解决?
答案 0 :(得分:0)
使用FLask cli
在命令行中,导航到包含项目的文件夹并运行:
export FLASK_APP=hello.py
flask run
注意你需要&#34; .py&#34;。应用程序运行脚本的名称应该是包含代码的脚本。在教程中有hello.py,但你的可能被称为MinimalApp.py或类似的。你需要有一个新的Flask版本,0.11或更高版本,因为这个cli是一个新功能。
使用app.run()代替新的Flask cli
首先,摆脱文件夹中的所有内容或启动新文件夹。接下来,确保您可以运行
import flask
在Python REPL中没有任何错误(在终端中键入&#34; python&#34;以访问REPL)。创建一个名为run.py的文件,并将以下内容粘贴到其中:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def IsSelf() :
return 'Hello 这是自'
app.run()
保存。在终端中,导航到包含该文件的文件夹并运行
python run.py