烧瓶错误=错误:无法导入“ web_app”

时间:2020-06-20 18:08:48

标签: python-3.x flask

此错误是反复出现且看似随机发生的,我始终很难修复它。我已经阅读了有关此问题的所有其他帖子,但仍然无法解决。

我的目录布局如下

> /data
>     .gitignore
>     Pipfile
>     Pipfile.lock
>     README.md
>     run.py
>     /web_app
>         /models
>         /routes
>         /templates
>         init.py

这是init.py

from flask import Flask
from web_app.routes.home_routes import home_routes
from web_app.routes.json_routes import json_routes
from web_app.routes.insert_routes import insert_routes

app = Flask(__name__)

app.register_blueprint(home_routes)
app.register_blueprint(json_routes)
app.register_blueprint(insert_routes)

app.run(debug=True)

在这里运行。py

from web_app import app
  1. 我打开/ data
  2. 我执行:设置FLASK_APP = web_app
  3. 我执行:运行flask
  4. 我收到这个常见错误:

错误:无法导入“ web_app”。

我以为我已经完全完成了烧瓶的必要目录设计,但是它仍然存在。

3 个答案:

答案 0 :(得分:0)

尝试重命名 init.py / data / web_app目录中的文件 __ init __。py

答案 1 :(得分:0)

我想我知道出了什么问题。当我在web_app目录中运行flask运行时,它就起作用了。尽管我仍然不了解flask如何读取/运行flask目录的逻辑,但是我担心它还会再次发生。

答案 2 :(得分:0)

尝试将init.py重命名为__init__.py并导入为

from .web_app import app
相关问题