Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To set up isort to only sort within sections and leave existing sections as they are, you can use the "--profile" flag to create a custom configuration file and then modify the configuration as needed. Here are the steps you can follow:

  1. Run isort --profile=create to generate a default configuration file named pyproject.toml.
  2. Open pyproject.toml in a text editor and locate the [tool.isort] configuration section.
  3. Add the following settings to the [tool.isort] section to enable section sorting and prevent isort from creating new sections:
force_sort_within_sections = true
force_single_line = true
  1. Optionally, you can also add the known_first_party and known_third_party settings to specify the names of your project's modules and external packages, respectively. This will help isort group your imports more intelligently.
  2. Save the pyproject.toml file and run isort again on your project directory. Isort will now only sort import statements within existing sections and leave the rest of the file's structure unchanged.

Note that you may need to adjust the configuration settings depending on the specific structure of your codebase. For more information on all the available settings, see the isort documentation.