Python模块在底部导入可以吗?

huangapple go评论81阅读模式
英文:

Is a Python module import at the bottom ok?

问题

Pylint在我的__init__.py末尾放置from .views import *时指责我,说导入应该放在模块的顶部。

如果我将其放在__init__.py的顶部,那么Flask无法找到我的路由(视图),因此无法正常工作。页面不加载,出现404错误。将路由在末尾导入时可以正常加载。

一些问题:

  • Pylint是否错误地假设模块应该始终放在顶部,而在某些情况下在末尾导入(就像在这种情况下)是可以接受的?
  • 还有没有其他导入路由的方法?

参考示例:

.
├── README.md
├── my_app
│   ├── __init__.py
│   ├── forms.py
│   ├── models.py
│   ├── static
│   ├── templates
│   │   ├── index.html
│   │   └── loggedin.html
│   └── views.py
├── config.py
├── instance
│   └── config.py
├── requirements.txt
└── run.py

__init__.py中的示例代码:

from flask import Flask, render_template
from authlib.integrations.flask_client import OAuth


app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')

oauth = OAuth(app)

APP_CALLBACK_URL = app.config['APP_CALLBACK_URL']
APP_CLIENT_ID = app.config['APP_CLIENT_ID']
APP_CLIENT_SECRET = app.config['APP_CLIENT_SECRET']
APP_DOMAIN = app.config['APP_DOMAIN']
APP_BASE_URL = 'https://' + APP_DOMAIN

my_app = oauth.register(
    'MY_APP',
    client_id=APP_CLIENT_ID,
    client_secret=APP_CLIENT_SECRET,
    api_base_url=APP_BASE_URL,
    access_token_url=APP_BASE_URL + '/oauth/token',
    authorize_url=APP_BASE_URL + '/authorize',
)

from .views import *
英文:

Pylint is yelling at me for putting a from .views import *at the end of my __init__.pysaying imports should be placed at the top of the module.

If I place it at the top of __init__.py then Flask can't find my routes (views) so that doesn't work. Page doesn't load, 404 error. Loads fine when routes are imported at the end.

A couple questions:

  • Is Pylint wrong to assume that modules should always go at the top and it's acceptable sometimes to import at the end (like in this case)?
  • or is there another way I should be importing my routes?

for reference, in case:

.
├── README.md
├── my_app
│   ├── __init__.py
│   ├── forms.py
│   ├── models.py
│   ├── static
│   ├── templates
│   │   ├── index.html
│   │   └── loggedin.html
│   └── views.py
├── config.py
├── instance
│   └── config.py
├── requirements.txt
└── run.py

example of what's in __init__.py

from flask import Flask, render_template
from authlib.integrations.flask_client import OAuth


app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')

oauth = OAuth(app)

APP_CALLBACK_URL = app.config['APP_CALLBACK_URL']
APP_CLIENT_ID = app.config['APP_CLIENT_ID']
APP_CLIENT_SECRET = app.config['APP_CLIENT_SECRET']
APP_DOMAIN = app.config['APP_DOMAIN']
APP_BASE_URL = 'https://' + APP_DOMAIN

my_app = oauth.register(
    'MY_APP',
    client_id=APP_CLIENT_ID,
    client_secret=APP_CLIENT_SECRET,
    api_base_url=APP_BASE_URL,
    access_token_url=APP_BASE_URL + '/oauth/token',
    authorize_url=APP_BASE_URL + '/authorize',
)


from .views import *

答案1

得分: 5

一般来说,导入应该放在顶部,但Flask文档谈到了这种情况,并鼓励您像您所做的那样操作。来自https://flask.palletsprojects.com/en/1.1.x/patterns/packages/:

  1. Flask应用程序对象的创建必须在__init__.py文件中进行。这样每个模块都可以安全地导入它,并且__name__变量将解析为正确的包。

  2. 所有视图函数(具有route()装饰器的函数)必须在__init__.py文件中导入。不是对象本身,而是它所在的模块。在创建应用程序对象之后导入视图模块。

顺便说一下,不要使用from .views import *,而应该使用import .views

英文:

Generally speaking, imports should go at the top, but Flask documentation talks about this kind of situations and encourages you to do as you did. Taken from https://flask.palletsprojects.com/en/1.1.x/patterns/packages/:

> 1. the Flask application object creation has to be in the __init__.py file. That way each module can import it safely and the __name__ variable will resolve to the correct package.
>
> 2. all the view functions (the ones with a route() decorator on top) have to be imported in the __init__.py file. Not the object itself, but the module it is in. Import the view module after the application object is created.

By the way, don't do from .views import *. Do import .views instead.

huangapple
  • 本文由 发表于 2020年1月3日 19:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定