Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import statements should be after any shebang line #18

Open
daira opened this issue Aug 13, 2014 · 8 comments
Open

import statements should be after any shebang line #18

daira opened this issue Aug 13, 2014 · 8 comments
Assignees
Milestone

Comments

@daira
Copy link
Contributor

daira commented Aug 13, 2014

+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 mitsuhiko/python-modernize#21 )

@daira
Copy link
Contributor Author

daira commented Aug 28, 2014

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 #.

@daira
Copy link
Contributor Author

daira commented Aug 28, 2014

(A module docstring is defined as a string at top-level that precedes any other code.)

@takluyver takluyver mentioned this issue Oct 13, 2014
@daira daira modified the milestones: Release 0.5, Release 0.4 Oct 14, 2014
@daira
Copy link
Contributor Author

daira commented Oct 14, 2014

This only affects scripts; it can wait until the next release.

@techtonik
Copy link
Contributor

I would say that this can be simplified to "just before any real Python code that is not merely a string".

@daira
Copy link
Contributor Author

daira commented Oct 16, 2014

No, because of #19; I originally filed #19 because of such a case in Tahoe-LAFS.

@takluyver
Copy link
Contributor

I think the ordering should be:

  1. Shebang
  2. Encoding comment
  3. Module docstring
  4. __future__ imports
  5. Regular imports
  6. 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.

@techtonik
Copy link
Contributor

@daira don't treat unicode strings as "merely a string" and that's it.

@daira
Copy link
Contributor Author

daira commented Nov 25, 2014

mitsuhiko/python-modernize#30 was a duplicate (see also django-wiki/django-wiki#33 ).

@daira daira self-assigned this Jan 18, 2015
jeremyh added a commit to jeremyh/python-modernize that referenced this issue May 27, 2015
Addresses issue PyCQA#18

Imports are also placed after any comments (such as copyright notices), to be consistent with the existing import placement in files with module docstrings.
jeremyh added a commit to jeremyh/python-modernize that referenced this issue May 27, 2015
…space.

Addresses issue PyCQA#18

Imports are already placed after copyright/comments if there's a
docstring or shebang in the file. This makes import handling consistent in more cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants