[英]How can I import app from __init__.py and why I'm getting this error?
I want to import db
from __init__.py
int the model.py
file but I can't import, an error appears that says:我想从
__init__.py
导入db
到model.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'
My application tree:我的应用程序树:
___ 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: __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
Here you can see I've db.init_app(app)
but still I'm getting ImportError
.在这里你可以看到我有
db.init_app(app)
但我仍然收到ImportError
。
After adding __init__.py
file inside the bytewar.user/
and bytewar.admin/
(beside views.py
), I'm getting error in run.py
:在
bytewar.user/
和bytewar.admin/
(在views.py
旁边)中添加__init__.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'
run.py:运行.py:
from bytewar import app
if __name__ == "__main__":
app.run()
My bytewar/user/__init__.py
and as it is bytewar/admin/__int__.py
:我的
bytewar/user/__init__.py
和bytewar/admin/__int__.py
:
from flask import Blueprint
user_blueprint = Blueprint(
'user', __name__,
static_folder='static',
template_folder='template',
)
Why is this happening?为什么会这样?
In bytewar.user.views
you import db from your __init__.py
.在
bytewar.user.views
您从__init__.py
导入 db。 That however is only defined a few lines below the import.然而,这仅在导入下方定义了几行。 So at the time the import is done, db does not exist yet, which the error confirms.
所以在导入完成时, db 还不存在,错误确认了这一点。 When you look at the example code for application factory 1 you see that the
app
variable is only used after it is defined, your code should do that too.当您查看应用程序工厂1的示例代码时,您会看到
app
变量仅在定义后使用,您的代码也应该这样做。 Move your imports to the bottom and you are set.将您的导入移动到底部并设置好。
As an aside: Why do you create your app
in the factory and immediately overwrite it?顺便说一句:为什么要在工厂中创建
app
并立即覆盖它? Leave the second creation out.留下第二个创作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.