Ask Your Question
1

How can I set up isort to sort only within sections and leave existing sections as they are?

asked 2023-01-19 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-08-14 16:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-19 11:00:00 +0000

Seen: 10 times

Last updated: Aug 14 '22