9

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?
golmschenk
  • 11,736
  • 20
  • 78
  • 137

1 Answers1

10
  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
Zhenhua
  • 2,389
  • 15
  • 10
insecure
  • 552
  • 4
  • 10
  • Thanks for the response. Your (1) is actually in support of *not* putting the __author__ below the imports. Just below the docstrings and module comments. So from that it looks like it should appear above the imports. For (2) the PyCharm "Python Script" template only contains the __author__ entry and nothing else. What change would need to be made there to make it appear above automatically added imports? (say, when you tell PyCharm to automatically add an import based on what function you're using in the code) – golmschenk Jul 23 '14 at 00:59
  • 1
    I don't think this is possible. Another problem that would need to be addressed is the fact that __future__ **must** be imported before anything else, even before other imports. Why do you want to use \__author__ anyway? Personally I do not use it at all because I do not see any good reason for doing so. – insecure Jul 23 '14 at 08:12
  • I think the argument that there's no real good reason to use _author_ at all is probably correct. Unfortunately, that doesn't actually answer the original question, so I'll certainly leave this question around for future visitors and possibly other answers. – golmschenk Jul 29 '14 at 04:25
  • 1
    @insecure - But another section on this page tells something different: https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names! *Module level "dunders" [...] such as ``__all__`` , ``__author__`` , ``__version__`` , etc. should be placed after the module docstring but before any import statements except from __future__ imports*, what about this? – linusg Aug 19 '16 at 15:44
  • 4
    PEP 8 has been [updated in June 2016](http://bugs.python.org/issue27187), and dunder module globals now explicitly are placed *before* the imports (but after any `from __future__` imports). See revs [cf8e888b9555](https://hg.python.org/peps/rev/cf8e888b9555) and [c451868df657](https://hg.python.org/peps/rev/c451868df657) and [Coding style (PEP8) - Module level "dunders"](https://stackoverflow.com/q/39044343). – Martijn Pieters Aug 19 '16 at 17:17