Python 3.x 在Python3脚本中导入模块时出现问题
我遇到以下错误:ImportError:在我进入模块blueprint.py后,无法从“admin.blueprint”导入名称“admin”。请帮助我了解应用程序的结构,以避免将来出现此类错误Python 3.x 在Python3脚本中导入模块时出现问题,python-3.x,flask,Python 3.x,Flask,我遇到以下错误:ImportError:在我进入模块blueprint.py后,无法从“admin.blueprint”导入名称“admin”。请帮助我了解应用程序的结构,以避免将来出现此类错误 [错误][1] [结构][2] [1]: https://i.stack.imgur.com/cgrbH.png [2]: https://i.stack.imgur.com/fDr7O.png您是否尝试过创建以下应用程序? 如果你刚开始工作,我强烈建议你遵循这个结构 情况如下: /home/us
[错误][1]
[结构][2]
[1]: https://i.stack.imgur.com/cgrbH.png
[2]: https://i.stack.imgur.com/fDr7O.png您是否尝试过创建以下应用程序? 如果你刚开始工作,我强烈建议你遵循这个结构 情况如下:
/home/user/Projects/flask-tutorial
├── flaskr/
│ ├── __init__.py
│ ├── db.py
│ ├── schema.sql
│ ├── auth.py
│ ├── blog.py
│ ├── templates/
│ │ ├── base.html
│ │ ├── auth/
│ │ │ ├── login.html
│ │ │ └── register.html
│ │ └── blog/
│ │ ├── create.html
│ │ ├── index.html
│ │ └── update.html
│ └── static/
│ └── style.css
├── tests/
│ ├── conftest.py
│ ├── data.sql
│ ├── test_factory.py
│ ├── test_db.py
│ ├── test_auth.py
│ └── test_blog.py
├── venv/
├── setup.py
└── MANIFEST.in
如果您希望它在当前应用程序中运行,我建议您在与您的main.py
相同的文件夹中创建一个admin.py
from flask import Blueprint, redirect, render_template, request, url_for
from config import ConfigAdmin
from models import Post
bp = Blueprint('admin', __name__, template_folder='templates', static_folder='static')
然后将其导入到main.py
中,如下所示
from admin import bp as adminbp
app.register_blueprint(adminbp, url_prefix='/admin')
欢迎来到SO。请以文本而不是图像的形式发布错误。你的目录结构是什么样的?如果blueprint.py文件放在main.py旁边,那么会出现完全相同的错误。我说创建一个
admin.py
文件?
/home/user/Projects/flask-tutorial
├── flaskr/
│ ├── __init__.py
│ ├── db.py
│ ├── schema.sql
│ ├── auth.py
│ ├── blog.py
│ ├── templates/
│ │ ├── base.html
│ │ ├── auth/
│ │ │ ├── login.html
│ │ │ └── register.html
│ │ └── blog/
│ │ ├── create.html
│ │ ├── index.html
│ │ └── update.html
│ └── static/
│ └── style.css
├── tests/
│ ├── conftest.py
│ ├── data.sql
│ ├── test_factory.py
│ ├── test_db.py
│ ├── test_auth.py
│ └── test_blog.py
├── venv/
├── setup.py
└── MANIFEST.in
from flask import Blueprint, redirect, render_template, request, url_for
from config import ConfigAdmin
from models import Post
bp = Blueprint('admin', __name__, template_folder='templates', static_folder='static')
from admin import bp as adminbp
app.register_blueprint(adminbp, url_prefix='/admin')