PyCharm - Have author appear before imports?

心不动则不痛 提交于 2019-12-09 09:30:31

问题


When you create new python files and add new imports, PyCharm will automatically add the imports and __author__ tag whenever it can by itself. However, by default the __author__ tag will always appear below any imports. It seems to me that the __author__ tag should be up at the top of the file where I would also put things like docstrings. This way everything describing the file is at the top, then the actual code (including the imports) is below that.

So two questions:

  1. Is there a good reason for putting the __author__ tag below the imports?
  2. How can I set PyCharm to put the __author__ tag above the imports by default?

回答1:


  1. is according to the "Imports" section of PEP-8:

    Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants

    __author__ is a global "variable" and should therefore appear below the imports.

  2. You may go to Pycharm's settings (Ctrl-Alt-S), choose "File and Code Templates" and adjust the "Python Script" template to your liking.

Pls Note: As mentioned by Martijn's comment below, item 1 of the above statement is no longer true as PEP8 has been updated (in June 2016) https://www.python.org/dev/peps/pep-0008/#id24 with a good example

"""
This is the example module.
This module does stuff.
"""

from __future__ import barry_as_FLUFL

__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'

import os
import sys


来源:https://stackoverflow.com/questions/24741141/pycharm-have-author-appear-before-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!