执行flask run之后,提示:No module named 'blueprints'

我的代码参考自bluelog,但结构有些差别。
完整错误打印:
(venv) jerryxin@jerryxin-ThinkPad-Edge-E530:~/PycharmProjects/flaskblog$ flask run

  • Serving Flask app “.” (lazy loading)
  • Environment: development
  • Debug mode: on
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 100-198-447
    127.0.0.1 - - [22/May/2019 15:46:13] “GET / HTTP/1.1” 500 -
    Traceback (most recent call last):
    File “/home/jerryxin/PycharmProjects/flaskblog/init.py”, line 2, in
    from blueprints.admin import admin_bp
    ImportError: No module named ‘blueprints’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 325, in call
self._flush_bg_loading_exception()
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/_compat.py”, line 35, in reraise
raise value
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 302, in _load_app
self._load_unlocked()
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 317, in _load_unlocked
self._app = rv = self.loader()
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 372, in load_app
app = locate_app(self, import_name, name)
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 242, in locate_app
‘\n\n{tb}’.format(name=module_name, tb=traceback.format_exc())
flask.cli.NoAppException: While importing “flaskblog”, an ImportError was raised:

Traceback (most recent call last):
File “/home/jerryxin/PycharmProjects/flaskblog/venv/lib/python3.5/site-packages/flask/cli.py”, line 235, in locate_app
import(module_name)
File “/home/jerryxin/PycharmProjects/flaskblog/init.py”, line 2, in
from blueprints.admin import admin_bp
ImportError: No module named ‘blueprints’
127.0.0.1 - - [22/May/2019 15:46:13] “GET /?debugger=yes&cmd=resource&f=style.css HTTP/1.1” 200 -
127.0.0.1 - - [22/May/2019 15:46:13] “GET /?debugger=yes&cmd=resource&f=debugger.js HTTP/1.1” 200 -
127.0.0.1 - - [22/May/2019 15:46:13] “GET /?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1” 200 -
127.0.0.1 - - [22/May/2019 15:46:13] “GET /?debugger=yes&cmd=resource&f=console.png HTTP/1.1” 200 -

目录结构:
├── apis
│ └── v1
├── blueprints
│ ├── admin.py
│ ├── auth.py
│ ├── blog.py
│ └── pycache
├── emails.py
├── extensions.py
├── forms.py
├── init.py
├── models.py
├── Pipfile
├── Pipfile.lock
├── pycache
│ ├── extensions.cpython-35.pyc
│ ├── init.cpython-35.pyc
│ └── setting.cpython-35.pyc
├── README.md
├── setting.py
├── static
│ ├── admin
│ ├── auth
│ └── blog
├── templates
│ ├── 404.html
│ ├── 500.html
│ ├── admin
│ ├── auth
│ └── blog
└── venv
├── bin
├── include
├── lib
├── lib64 -> lib
├── pyvenv.cfg
└── src

报错代码:
from flask import Flask
from blueprints.admin import admin_bp
from blueprints.auth import auth_bp
from blueprints.blog import blog_bp
from setting import config
from extensions import bootstrap, db, moment, ckeditor, mail
from apis.v1 import api_v1
import os

def create_app(config_name=None):
if config_name is None:
config_name = os.getenv(‘FLASK_CONFIG’, ‘development’)
else:
print(‘config_name::::%s’, config_name)
app = Flask(‘flaskblog’)
app.config.from_object(config[config_name])

register_logging(app)  # 注册日志处理器
register_extensions(app)  # 注册扩展(扩展初始化)
register_blueprints(app)  # 注册蓝本
register_commands(app)  # 注册自定义shell命令
register_errors(app)  # 注册错误处理函数
register_shell_context(app)  # 注册shell上下文处理函数
register_template_context(app)  # 注册模板上下文处理函数

blueprints文件夹里没有__init__.py文件?

加一个blueprints/__init__.py试试,这是标志blueprints文件夹是一个Python包。

另外,请学习下插入代码块的方式。


插入代码块的方式:除了将所有代码缩进四格,更方便的插入代码块方式是使用三个连续的反引号作为开始和结束标记,比如:

```
def hello():
    return ‘Hello World’
```

会被渲染为:

 def hello():
     return 'Hello World'

具体请看置顶帖子 技术提问帖发帖规则(创建提问帖前必读)

感谢回复。已经解决了,模块引用路径问题导致。改成如下后正常。

from flask import Flask
from .blueprints.admin import admin_bp
from .blueprints.auth import auth_bp
from .blueprints.blog import blog_bp
from .setting import config
from .extensions import bootstrap, db, moment, ckeditor, mail
from .apis.v1 import api_v1

这样也行,本来我也是想说前面加点.的(相对导入),不过回头看了下李辉老师的工程,是不需要加点。总之这一块如果你没有弄明白,下次还会遇到。

官方参考资料:https://docs.python.org/zh-cn/3/reference/import.html#packages