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

标签 python python-3.x flask import importerror

我想从 __init__.py 导入 db int 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'

我的应用程序树:

___ 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

bytewar.user/bytewar.admin/ 中添加 __init__.py 文件后(在 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'

run.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导入数据库。然而,这仅在导入下方定义了几行。因此,在导入完成时,数据库尚不存在,错误证实了这一点。当您查看应用程序工厂 1 的示例代码时您会看到 app 变量仅在定义后才使用,您的代码也应该这样做。将导入移至底部即可完成设置。

顺便说一句:为什么要在工厂中创建您的应用并立即覆盖它?将第二个创作排除在外。

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

相关文章:

python - Django try/except on DoesNotExist 仍然抛出它

c++ - 在 Linux 上用 C++ 编写类似 pexpect 的程序

python - "Directory migrations already exists"在 Heroku 上初始化期间

python - pandas:输出两列字符串列表之间的差异

python - pandas 中带有内部连接的 where 条件

python - 如何将嵌套的字典键和值平坦化为其类型和变量的平坦列表?

python - "Protocols cannot be used with isinstance()"- 为什么不呢?

python - python3.2有python-notify模块吗?

python - flask (Python): Pass input as parameter to function of different route with fixed URL

python - Gunicorn 和主管背后的 Flask - 记录所有请求和响应