Sunday, June 15, 2008

Organizing imports

One of the features I can't live without in Pydev Extensions is the auto-import. Basically, when you want to get a class/method into the namespace, just start writing it, request a completion and the class will be completed and an import for it will be added at the top-level part of the file (it's similar to what JDT does).

So, after using this feature lots of times, one thing that started happening is that some modules would end with imports such as:

from mod1 import Class1
from mod1 import Class2
from mod1 import Class3

Now, on version 1.3.18, the auto-import will group imports when requested, so, after making those requests it'd become:

from mod1 import Class1, Class2, Class3

But that won't group existing imports... For that, the organize imports action was improved -- until now, Ctrl+Shift+O would only sort the imports alphabetically, but from now on it'll also group them, and if Pydev Extensions is available, for each undefined token found in the code analysis, an option to add an import to one of the available definitions of the token will be presented -- which is especially useful when doing refactorings and cutting/copying code from one place to another.

3 comments:

mclin said...

That's awesome. I love autoimport. And Organize Imports is almost to the point of completely abstracting away imports management!

Something that would be nice though, have you thought about splitting imports into different sections? eg. Separating standard library imports from the rest.

Fabio Zadrozny said...

Actually, currently there are no plans of separating imports into different sections... you can enter a feature request if you'd like to have it -- or send a patch, as that part is in the open-source version ;-)

Cheers,

Fabio

Anonymous said...

Cool feature and I must admit I use it quite often as well. Many thanks to Fabio and chaps for such a great tool!

Unfortunately, my teamleader isn't happy about my using it because pep-0008

http://www.python.org/dev/peps/pep-0008/

says to organize imports other way round:

Imports should be grouped in the following order:

1. standard library imports
2. related third party imports
3. local application/library specific imports

etc...

Is it possible to make a change in pydev to sort imports this way?