我甚至不确定这里发生了什么事。
我正在尝试初始化我的数据库在一个烧瓶应用程序。
每当我尝试运行init-db (在我的程序中它是init-db2,因为我创建了第二个db文件并删除了第一个)它。
。
这对我来说没有任何意义,因为我没有flaskr.py文件,但是它是我的一个应用程序目录的名称,所以我认为可能会出现一些混乱吗?
我的环境被设置得很好
设置FLASK_APP=app.py集FLASK_ENV=development python -m烧瓶init-db2
我的db2文件也与本教程内联。
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_db():
db = get_db()
with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
@click.command('init-db2')
@with_appcontext
def init_db_command():
"""Clear 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)我的init文件似乎也符合本教程的要求。
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
from . import db2
db2.init_app(app)
return appPS C:\Windows\System32\ShoeManagementApp\Flaskr> set FLASK_APP=app.py
PS C:\Windows\System32\ShoeManagementApp\Flaskr> PS C:\Windows\System32\ShoeManagementApp\Flaskr> set FLASK_ENV=development
At line:1 char:1
+ PS C:\Windows\System32\ShoeManagementApp\Flaskr> set FLASK_ENV=develo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
PS C:\Windows\System32\ShoeManagementApp\Flaskr> PS C:\Windows\System32\ShoeManagementApp\Flaskr> python -m flask init-db2
At line:1 char:1
+ PS C:\Windows\System32\ShoeManagementApp\Flaskr> python -m flask init ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
PS C:\Windows\System32\ShoeManagementApp\Flaskr> Error: While importing 'Flaskr.app', an ImportError was raised.set FLASK_APP=app.py
At line:1 char:1
+ Error: While importing 'Flaskr.app', an ImportError was raised.set FL ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Error::String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException我认为可能导致这个问题但不知道如何解决的唯一原因是,我使用的是以前DB设置中的SQL炼金术,现在就像教程中的sqlite一样,我不知道导入这两者是否会导致冲突。
发布于 2022-01-12 11:03:55
那么,您似乎要在PS中执行以下命令:
PS C:\Windows\System32\ShoeManagementApp\Flaskr> python -m flask init-db2这可能是复制粘贴错误,您要求使用命令执行提示符。只是问题:python -m flask init-db2它应该做的工作!
https://stackoverflow.com/questions/70661666
复制相似问题