Let’s go through the steps to configure and format Python code in IntelliJ IDEA. Note that IntelliJ IDEA is a versatile IDE and not specifically tailored for Python. Therefore, you’ll need to install and configure the Python plugin for IntelliJ IDEA.
First, let’s install and configure the Python plugin:
- Open IntelliJ IDEA.
- From the
Welcomescreen, selectConfigure->Plugins. - In the
Pluginswindow, click on theMarketplacetab. - In the search bar, type
Pythonand pressEnter. - From the list of plugins, find the
Python Community Editionplugin and click onInstall. - Once the installation is complete, you will be prompted to restart IntelliJ IDEA. Click on
Restart.
Once you have the Python plugin installed and configured, you can now set up automatic formatting. The PEP 8 style guide for Python code is widely used and can be configured in IntelliJ IDEA.
Let’s configure automatic code formatting using PEP 8:
- Open IntelliJ IDEA.
- From the
Filemenu, selectSettings(for Windows and Linux) orPreferences(for macOS). - In the
Settings/Preferenceswindow, in the left pane, navigate toEditor->Code Style->Python. - In the
Pythonpane, navigate to thePEP 8tab. - Check the box next to
Optimize importsandEnsure right margin is not exceeded. - Set the
Right margin (columns)to 79, as recommended by PEP 8. - Click on
Applyand thenOK.
Now you can format your Python code using PEP 8. You can either format a part of the code, a whole file, or a whole project.

Here is how to format code:
- To format a part of the code, highlight the part you want to format, then from the
Codemenu, selectReformat Codeor use the shortcutCtrl+Alt+L(for Windows and Linux) orCmd+Option+L(for macOS). - To format a whole file, don’t highlight anything, just click somewhere in the file you want to format, then from the
Codemenu, selectReformat Codeor use the shortcutCtrl+Alt+L(for Windows and Linux) orCmd+Option+L(for macOS). - To format a whole project, from the
Codemenu, selectReformat Code. In theReformat Filedialog, selectWhole project, then click onRun.
Remember to always verify your code after formatting since it may sometimes lead to unintended changes.
