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

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

Is a Python module import at the bottom ok?

问题

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

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

一些问题:

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

参考示例:

  1. .
  2. ├── README.md
  3. ├── my_app
  4. ├── __init__.py
  5. ├── forms.py
  6. ├── models.py
  7. ├── static
  8. ├── templates
  9. ├── index.html
  10. └── loggedin.html
  11. └── views.py
  12. ├── config.py
  13. ├── instance
  14. └── config.py
  15. ├── requirements.txt
  16. └── run.py

__init__.py中的示例代码:

  1. from flask import Flask, render_template
  2. from authlib.integrations.flask_client import OAuth
  3. app = Flask(__name__, instance_relative_config=True)
  4. app.config.from_object('config')
  5. app.config.from_pyfile('config.py')
  6. oauth = OAuth(app)
  7. APP_CALLBACK_URL = app.config['APP_CALLBACK_URL']
  8. APP_CLIENT_ID = app.config['APP_CLIENT_ID']
  9. APP_CLIENT_SECRET = app.config['APP_CLIENT_SECRET']
  10. APP_DOMAIN = app.config['APP_DOMAIN']
  11. APP_BASE_URL = 'https://' + APP_DOMAIN
  12. my_app = oauth.register(
  13. 'MY_APP',
  14. client_id=APP_CLIENT_ID,
  15. client_secret=APP_CLIENT_SECRET,
  16. api_base_url=APP_BASE_URL,
  17. access_token_url=APP_BASE_URL + '/oauth/token',
  18. authorize_url=APP_BASE_URL + '/authorize',
  19. )
  20. 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:

  1. .
  2. ├── README.md
  3. ├── my_app
  4. │   ├── __init__.py
  5. │   ├── forms.py
  6. │   ├── models.py
  7. │   ├── static
  8. │   ├── templates
  9. │   │   ├── index.html
  10. │   │   └── loggedin.html
  11. │   └── views.py
  12. ├── config.py
  13. ├── instance
  14. │   └── config.py
  15. ├── requirements.txt
  16. └── run.py

example of what's in __init__.py

  1. from flask import Flask, render_template
  2. from authlib.integrations.flask_client import OAuth
  3. app = Flask(__name__, instance_relative_config=True)
  4. app.config.from_object('config')
  5. app.config.from_pyfile('config.py')
  6. oauth = OAuth(app)
  7. APP_CALLBACK_URL = app.config['APP_CALLBACK_URL']
  8. APP_CLIENT_ID = app.config['APP_CLIENT_ID']
  9. APP_CLIENT_SECRET = app.config['APP_CLIENT_SECRET']
  10. APP_DOMAIN = app.config['APP_DOMAIN']
  11. APP_BASE_URL = 'https://' + APP_DOMAIN
  12. my_app = oauth.register(
  13. 'MY_APP',
  14. client_id=APP_CLIENT_ID,
  15. client_secret=APP_CLIENT_SECRET,
  16. api_base_url=APP_BASE_URL,
  17. access_token_url=APP_BASE_URL + '/oauth/token',
  18. authorize_url=APP_BASE_URL + '/authorize',
  19. )
  20. 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:

确定