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

isort should not move imports past other code #693

Closed
benjaminjkraft opened this issue Apr 13, 2018 · 2 comments
Closed

isort should not move imports past other code #693

benjaminjkraft opened this issue Apr 13, 2018 · 2 comments

Comments

@benjaminjkraft
Copy link

Currently, if you have a file like:

import sys

sys.path.insert(...)

import foo
import bar

then isort will move the two later imports up to go above the sys.path line. But you probably put it after the sys.path line because you need that path modification to find foo!

What I would expect isort to do here is to treat two import blocks that have a non-import statement in between as completely separate: that is, it should sort the block with import sys (trivial), and separately sort the block with import foo and import bar (swap the two), but not mix those two blocks together.

(This behavior could, of course, be optional, although I'd argue it's the right behavior for most cases; it's not the job of isort to decide whether having that code in the middle of your imports was a good idea. A linter is better suited to do that; and it would allow you to # noqa the offending line if you really do want it.)

@benjaminjkraft
Copy link
Author

Oops, I missed #468; this is a duplicate of it.

@antoine-gallix
Copy link

While waiting for the release of the features that'll allow it, here is my workaround. I put the patching and path altering commands their own module, so they are triggered by an import statement in the original file. Then using isort configuration, one can control the position of this import, for example making it it's own section and controlling the order of it.

Before:

# file: main.py
import sys
sys.path.append('.vendor')
import my_vendorized_module

After:

# file: main.py
import setuppath
import my_vendorized_module

# file: setuppath.py
import sys
sys.path.append('.vendor')

Configure isort with:

# file: setup.cfg
[isort]
force_to_top=setuppath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants