像这种问题怎么破 module level import not at top of file 关于 PEP8

# config:utf-8
# file : main\__init__.py

from flask import Blueprint

main_app = Blueprint('main', __name__, template_folder='../views/')


from . import views, errors <- 这行:module level import not at top of file
# coding:utf-8
# file: main\views.py

from flask import url_for, redirect, render_template, session, request, abort, redirect
from app import db
from . import main_app as main <- 这行

@main.route('/test')
def t_test():
    return render_template('test.html')

Flask 应用 放到顶层在 views 和 errors 就报 can not import 'main'错误。放到底下吧,就不 PEP 了……

阅读 20.5k
2 个回答

我也遇到过,可以另外写一个模块,然后再这里import 进去就很符合PEP8了

例如 # new.py

# config:utf-8
# file : main\new.py
from flask import Blueprint

main_app = Blueprint('main', __name__, template_folder='../views/')

在原本的__init__.py里这样写:

# config:utf-8
# file : main\__init__.py
from .new import main_app
from . import views, errors

其他你就知道了,楼上老外再stackoverflow里,同理大概也是这个意思

推荐问题