我正在创建一个简单的Flask应用程序,这是我的文件夹结构:
.
├── app
│ ├── flaskr
│ │ ├── db.py
│ │ ├── __init__.py
│ │ └── usio.sql
│ ├── templates
│ │ ├── historical.html
│ │ └── index.html
│ ├── test_usio.py
│ ├── usio.db
│ ├── usio.json
│ ├── usio.py
│ ├── usio.sql
│ └── wsgi.py
├── logs
│ └── nginx
│ ├── access.log
│ └── error.log
├── usio.db
├── usio.json
└── usio.sql
在我的db.py
文件中,我具有以下代码:
def init_db():
db = get_db()
with current_app.open_resource('usio.sql') as f:
db.executescript(f.read().decode('utf8'))
@click.command('init-db')
@with_appcontext
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
click.echo('Initialized the database.')
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
我正在关注本教程db creation tutorial
我的烧瓶版本是1.0.2
但是,当我尝试使用此命令时,会从flaskr
文件夹中抛出该错误:
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/usio_flask/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/flask/cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/flask/cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/click/core.py", line 1061, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/click/core.py", line 1100, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/flask/cli.py", line 515, in get_command
rv = info.load_app().cli.get_command(ctx, name)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/flask/cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "/home/kristian/.virtualenvs/usio_flask/lib/python3.4/site-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
File "/home/kristian/usio/usio/app/flaskr/db.py", line 7, in <module>
@click.command('init-db')
NameError: name 'click' is not defined
这是我的__init__
文件夹中的flaskr
文件:
def create_app():
app = ...
# existing code omitted
from . import db
db.init_app(app)
return app
此外,在尝试使用flaskr
命令之前,我将自己放置在flask init-db
文件夹中,在运行export=FLASK_APP=db
之前我也使用了此文件夹
有什么想法吗?
答案 0 :(得分:2)
您在本教程中缺少import click
。