gpt4 book ai didi

python - 如何从 __init__.py 导入应用程序以及为什么会出现此错误?

转载 作者:行者123 更新时间:2023-12-04 10:43:50 24 4
gpt4 key购买 nike

我要导入 db来自 __init__.pymodel.py文件,但我无法导入,出现错误提示:

Traceback (most recent call last):
File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
from bytewar import create_app
File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in <module>
from bytewar.user.views import user_blueprint
File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in <module>
from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in <module>
from bytewar import db
ImportError: cannot import name 'db'

我的应用程序树:
___ run.py     # <<------- ImportError in this file.
___ bytewar/
|__ __init__.py
|__ config.py
|__ model.py
|__ user/
| |__ __init__.py
| |__ views.py
| |__ static/
| | |__ img/
| | |__ ....
| |__ template/
| |__ index.html
| |__ about.html
|
|__ admin/
|__ __init__.py
|__ views.py
|__ template/
|__ index.html
|__ about.html


__init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
from bytewar.user.views import user_blueprint
from bytewar.admin.views import admin_blueprint

db = SQLAlchemy()

def create_app():
app = Flask('bytewar')
app = Flask(__name__.split('.')[0])
app.config.from_pyfile('config.py')
app.config['DEBUG'] = True

app.register_blueprint(user_blueprint)
app.register_blueprint(admin_blueprint)

login_manager = LoginManager()
bootstrap = Bootstrap(app)

db.init_app(app)
login_manager.init_app(app)
login_manager.session_protection = 'strong'

return app

在这里你可以看到我 db.init_app(app)但我仍然收到 ImportError .

添加后 __init__.py bytewar.user/ 内的文件和 bytewar.admin/ (在 views.py 旁边),我在 run.py 中遇到错误:
Traceback (most recent call last):
File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
from bytewar import app
File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in <module>
from bytewar.user.views import user_blueprint
File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in <module>
from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in <module>
from run import db
File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
from bytewar import app
ImportError: cannot import name 'app'

运行.py:
from bytewar import app


if __name__ == "__main__":
app.run()

我的 bytewar/user/__init__.py原样 bytewar/admin/__int__.py :
from flask import Blueprint

user_blueprint = Blueprint(
'user', __name__,
static_folder='static',
template_folder='template',
)

为什么会这样?

最佳答案

bytewar.user.views你从你的 __init__.py 导入数据库.然而,这仅在导入下方定义了几行。所以在导入完成时, db 还不存在,错误确认了这一点。当你查看应用工厂的示例代码时1你看到app变量仅在定义后使用,您的代码也应该这样做。将您的导入移动到底部并设置好。

顺便说一句:你为​​什么要创建你的 app在工厂并立即覆盖它?留下第二个创作。

关于python - 如何从 __init__.py 导入应用程序以及为什么会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59809861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com