import statements should be after any shebang line
+from __future__ import print_function
#!/usr/bin/env python
Imports should only be added after any initial lines that are either blank or start with #. That would account for copyright headers etc., as well as shebang lines.
(moved from https://github.com/mitsuhiko/python-modernize/issues/21 )
Taking into account #19, an import of a module that is not already imported at top-level should go:
- after existing
__future__imports, if any - otherwise, after the module docstring, if any
- otherwise, after any initial lines that are either blank or start with
#.
(A module docstring is defined as a string at top-level that precedes any other code.)
This only affects scripts; it can wait until the next release.
I would say that this can be simplified to "just before any real Python code that is not merely a string".
No, because of #19; I originally filed #19 because of such a case in Tahoe-LAFS.
I think the ordering should be:
- Shebang
- Encoding comment
- Module docstring
__future__imports- Regular imports
- General code
And any combination of the above can be omitted. I think the tricky thing is that it tries to add new imports at the end of the imports block, and that can go wrong if people mix other bits of code into their imports.
@daira don't treat unicode strings as "merely a string" and that's it.
https://github.com/mitsuhiko/python-modernize/issues/30 was a duplicate (see also https://github.com/django-wiki/django-wiki/pull/33 ).