PyCharm 2024.1 Help

Reformat and rearrange code

PyCharm lets you reformat your code according to the requirements you've specified in your current code style scheme or the .editorconfig file. If anything is not defined in .editorconfig, it is taken from the project settings.

You can reformat a part of code, the whole file, group of files, a directory, and a module. You can also exclude part of code or some files from the reformatting. PyCharm adheres to PEP8 rules and requirements for arranging and formatting Python code.

Reformat a code fragment

  1. In the editor, select a code fragment you want to reformat.

    Before reformatting, you can take a look at the code style settings that are applied to the selected code: press Alt+Enter and click Adjust code style settings.

  2. In the main menu, go to Code | Reformat Code or press Ctrl+Alt+L.

Reformat a file

  1. Either open your file in the editor and press Ctrl+Alt+Shift+L or in the Project tool window, right-click the file and select Reformat Code.

  2. In the Reformat File dialog that opens, if you need, select the following reformatting options:

    • Optimize imports: select this option if you want to remove unused imports, add missing ones, or organize import statements.

    • Rearrange entries: select this option if you need to rearrange your code based on the arrangement rules specified in the code style settings.

    • Code cleanup: select this option to run the code cleanup inspections.

    • Do not keep line breaks: reformat line breaks according to the code style settings. This option overrides the Keep when reformatting | Line breaks setting.

  3. Click Run.

    If you want to see the exact changes made to your code during the reformatting, use the Local History feature.

Reformat line indents

You can reformat line indents based on the specified settings.

  1. While in the editor, select the necessary code fragment and press Ctrl+Alt+I.

  2. If you need to adjust indentation settings, in the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style. Select the language for which you want to change indentation.

  3. On the Tabs and Indents tab, specify the appropriate indents options and click OK.

Automatically reformat code on save

You can configure the IDE to reformat code in modified files automatically when your changes are saved.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Tools | Actions on Save.

  2. Enable the Reformat code option.

  3. Additionally, you can configure the way the IDE will reformat your code:

    • Click Configure scope to specify the patterns of filenames and directories that you want to exclude from reformatting.

    • From the All file types list, select the types of files in which you want to reformat code.

    • Select Whole file or Changed lines if your project is under version control.

      If you select Changed lines, reformatting will apply only to the lines of code that have been changed locally, but not yet checked in to the repository.

Exclude files from reformatting

You can exclude a group of files and directories from reformatting, code arrangement, and import optimization.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style.

  2. Switch to the Formatter tab and in the Do not format field, enter the files and directories that you want to exclude using a glob pattern.

    You can specify several glob patterns separated with a semicolon ;. If you click the Expand icon, the field will expand, and each pattern will be shown on a separate line.

  3. Apply the changes and close the dialog.

    Specifying a glob pattern for excluding files from reformatting

Exclude code fragments from reformatting in the editor

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style.

  2. Switch to the Formatter tab and enable the Turn formatter on/off with markers in code comments option.

  3. In the editor, at the beginning of a region that you want to exclude, create a line comment Ctrl+/ and type @formatter:off. At the end of the region, create another line comment and type @formatter:on.

    The code between the markers won't be reformatted.

Keep existing formatting

You can select formatting rules which will be ignored when you reformat the code. For example, you can adjust the IDE to keep simple methods and functions in one line, whereas normally they are expanded into multiple lines after code reformatting.

  1. In the main menu, go to Settings | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab.

  2. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied.

  3. Reformat your code (Ctrl+Alt+L).

PyCharm will reformat your code in accordance with the current style settings, keeping existing formatting for the rules which you've selected.

Reformat Python code with Black

You can use Black to format your Python code in PyCharm. If you have Black installed in your system or in the project interpreter, you'll see ont of the following notifications:

  • Notification about Black executable detection
  • Notification about Black executable detection

Configure Black

  1. Do one of the following:

    • Click Configure in the notification popup.

    • Go to Tools | Black in the Settings dialog (Ctrl+Alt+S) .

  2. In the Execution mode drop-down, choose how PyCharm should run the Black formatter:

    • In Binary mode, the Black executable installed in your system will be used. If the executable is not found, you can specify the path by clicking the icon:

      Configuring Black formatter: binary mode
    • In Package mode, PyCharm uses the Black package installed in the project interpreter. You can install the Black package for the selected interpreter by clicking Install Black:

      Configuring Black formatter: package mode
  3. Choose when do you want to run the Black formatter:

    • Enable the On code reformat checkbox to use Black instead of the built-in formatter when you press Ctrl+Alt+L or select Code | Reformat Code from the main menu.

    • Select On save if you want to run Black formatter automatically when your changes are saved. To configure other actions, click All actions on save.

  4. The settings of the Black formatter can be configured in pyproject.toml. For Black v21.4.0 and higher, they are applied automatically. For more information, refer to the Black documentation.

    You can use the Settings field to specify additional options or override the settings from pyproject.toml:

    Specifying Black CLI options

    For more information, refer to the Black CLI reference.

Rearrange code

You can rearrange your code according to the arrangement rules set on the Code Style page of the Settings dialog.

Rearrange code entries

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style.

  2. Select a language for which you want to create arrangement rules.

  3. On the Arrangement tab, specify the appropriate options such as grouping and matching rules.

  4. Click OK to save the changes.

  5. In the editor, select the code entries you want to rearrange and go to Code | Rearrange Code in the main menu.

Last modified: 05 April 2024