Isort: Integration with PyCharm

Created on 20 Feb 2015  路  17Comments  路  Source: PyCQA/isort

Is it possible to integrate isort features to PyCharm ?

Most helpful comment

You can set up an external tool like this:
screen shot 2015-04-23 at 11 16 46 am
screen shot 2015-04-23 at 11 17 56 am
Then, you can even bind it to a shortcut for easy access:
screen shot 2015-04-23 at 11 20 45 am

All 17 comments

I'm interested in this as well.

Want to support PyCharm.

Super interested in this!

You can set up an external tool like this:
screen shot 2015-04-23 at 11 16 46 am
screen shot 2015-04-23 at 11 17 56 am
Then, you can even bind it to a shortcut for easy access:
screen shot 2015-04-23 at 11 20 45 am

awesome, thanks a lot for solution!

It does not work correctly if isort placed somewhere outside of project, i.e. ~/.virtualenvs/*
3rd party modules and project modules are merged together
Though I guess it is pycharm problem - it uses full path when running external program

isort does not always correctly detect which modules are third-party and which ones are yours. You can tell it to treat certain modules as third-party by passing -o module_name (-o is short for --thirdparty according to isort --help). I ended up creating an .isort.cfg in my project's top level folder which contains a section like this:

[settings]
known_third_party=caching,provider

This can then be nicely versioned and everybody in your team is on the same page. :-) From inside pycharm, I'm then invoking isort as

$PyInterpreterDirectory$/isort --settings-path $ProjectFileDir$ $FilePath$

Looking at the code, it treats all packages as third-party that have either site-packages or dist-packages in their import path. That probably doesn't include any packages that you installed via pip install -e.

+1 for the trick with settings

Thanks for $PyInterpreterDirectory$/isort
I have been running system wide isort which does not have many of project specific 3rd party modules that is why it treated them and project modules the same way.

As of #434, you can now also simply pass --virtual-env <dir> to isort, wherever it is installed

No longer needed, Pycharm does this itself now :)

@teeberg Update the solution with the description for "Output Filters setup" for console clickable links
https://www.jetbrains.com/help/pycharm/2017.2/add-edit-filter-dialog-2.html?utm_medium=help_link&utm_source=from_product&utm_campaign=PY&utm_content=2017.2

@Skyross my console output only shows:

/Users/jonas/code/project/env/bin/isort --settings-path /Users/jonas/code/project --virtual-env=/Users/jonas/code/project/env/bin/../ /Users/jonas/code/project/api.py

Process finished with exit code 0

which part of that do you want to make clickable?

@teeberg In a case of having any errors in sorting. You will get some output with file paths if you run without automatic reformatting. If you running any --diff command for example.

@Skyross I'm not sure if you're asking how to do that or if you're asking me to include those instructions in the guide above. If the former, adding this output filter did it for me:
image
However, the way I set up the example above, it always runs on the file that you currently have open anyway, so any filenames that could be matched in the output would just point back to the file that's already open. So I'm still not clear about what you're trying to accomplish. How did you set up isort in pycharm?

I was unable to set it up like that, because PyCharm kept running isort external tool without proper paths to virtual environment.

I needed to hardcode the path (think export PYTHONPATH=/Users/mpasternak/envs/bpp/lib/python3.7/site-packages/ ) in the shell script, then run that shell script, then run sort.

Without it, isort as ran by PyCharm was unable to import isort.main

If anybody got a cleaner solution, help welcome.

If I set up isort as an external tool it seems to be a general setting for PyCharm that is used across all projects I'm working on.
Is there a way to set project specific _working directory_ ??
Or is there a way to say: use the projects root directory ?

Was this page helpful?
0 / 5 - 0 ratings