pythonpycharmisort

How to set up filter for isort as external tool in PyCharm


I'm trying to set up isort as external tool in PyCharm. I'm unable to set up filter so that file paths are links.

Output from isort is:

ERROR: C:\dev\path\to\a\project\file.py Imports are incorrectly sorted.

According to docs putting $FILE_PATH$ should be sufficient yet it does not work for me. I've tried several regex styles without any success.


Solution

  • tl;dr use $FILE_PATH$(?<=\.py)( |$) as filter.

    So (^|[\W])(?<file>(?:\p{Alpha}\:|/)[0-9 a-z_A-Z\-\\./]+)(?<=\.py) is regexp used for $FILE_PATH. Source: https://github.com/JetBrains/intellij-community/blob/d29c4fa1a73e03b852353186d792540150336b9f/platform/lang-api/src/com/intellij/execution/filters/RegexpFilter.java#L39 See how it allows spaces in there?

    Meaning it will grab C:\dev\path\to\a\project\file.py Imports are incorrectly sorted. and as it does not point to a real file it won't be converted to a link.

    So you can either modify isort output format to something with clear filepath boundaries, or use something more fancy in regexp like positive look behind, which would make your filter look like this: $FILE_PATH$(?<=\.py)( |$)

    For testing java regexps you can use https://www.freeformatter.com/java-regex-tester.html if the provided filter does not meet your particular needs.