简体   繁体   English

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

[英]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导入dbmodel.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__.pybytewar/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.

相关问题 为什么我可以在没有__init__.py的情况下成功导入? - Why can I import successfully without __init__.py? 为什么即使使用__init__.py也不能从兄弟姐妹目录中导入软件包? - Why can't I import package from sibling directory in python even with use of __init__.py? 如何从 django 3.0 中的 __init__.py 中的 base.py 导入类。 似乎无法在应用程序注册时设置 self.apps_ready - How can I import class from base.py in __init__.py in django 3.0. It seems it isn't able to set self.apps_ready while app registry package 的导入错误,我是否缺少 __init__.py - Import error for package, am i missing an __init__.py Python - 为什么我可以导入没有__init__.py的模块? - Python - why can I import modules without __init__.py at all? flask:为什么我不能从 __init__.py 中的.views 或.models 导入? - flask: why cant I import from .views or .models in __init__.py? 如何使用 Flask 从 __init__.py 导入 - How to import from __init__.py with Flask Azure Functions:如何将其他文件夹中的类导入 Azure 时间触发器函数的 __init__.py 文件? - Azure Functions: How can I import classes from other folders to __init__.py file of an Azure time trigger function? 如何使用完整路径导入python包或__init__.py文件? - How can I import a python package or __init__.py file using the full path? 从__init__.py导入内容时,如何在pycharm中运行unittest - How do I get unittest run in pycharm when import something from __init__.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM