Contents
How to configure pycodestyle and autopep8 in PyCharm
   Jul 27, 2022     4 min read

Developers use apps to help us improve the quality of our code. As every programming language has its own standards and good practises. We can take advantage of that and set/follow rules that can be automatically analysed and improved by our IDE (Integrated Development Environment).

In our case, in order to follow the best practises recommended for our (python) code we use the Python standard known as PEP 8.

There are plenty of tools that can help you to follow this standard. We have chosen the following:

  • Linter: reviews the code and documentation syntax based on the standard pep8. We will use pycodestyle and pydocstyle
  • Formatter: automatically formats Python code. We will use autopep8, which uses the pycodestyle utility to determine what parts of the code need to be formatted to conform to the PEP 8 style guide.

In order to configure this in our IDE we can follow the next steps:

  1. Install the tools in your computer:
  2. Create in the root of your project a setup.cfg file which will contain the configuration that the linter will follow. Example of the syntax of this file:

    [pycodestyle]
    max-line-length = 120
    
    [pydocstyle]
    ignore = D100, D101, D102, D107
    
    • Line 1 indicates the linter (for the code) that will be used to analyse the code.
    • Line 2 (in this example) overrides the default config for the max line length and sets it to 120 chars.
    • Line 4 indicates the linter for the documentation that will be used to analyse the documentation.
    • Line 5 tells the linter to ignore some specific rules set by default (that how we can customise our analysis to ignore some rules that we disagree with or we want to ignore for the time being).

    If you have doubts of how to use these tools you can always go to the shell/bash/terminal in your computer and use the help documentation in order to see the options you have got to add to this setup file. Typing for example:

     pydocstyle --h
    
  3. We need to define the linter (pycodestyle) as an External Tool. (Preferences → Tools → External Tools). Click on the + symbol. (A new window will be opened. The config below needs to be applied)

    plus_external_tools.png

    • Name: your desired Name
    • Description: your description
    • Program: the location of the linter in your computer. (You can use the command where in bash to get it)
    • Arguments: $FilePath$
    • Working directory: $ProjectFileDir$

    external_tool_linter.png

  4. Same with pydocstyle:

    external_tool_pydocstyle.png

  5. Same with autopep8, but with some changes in the values:
    • Name: your desired Name
    • Description: your description
    • Program: the location of the formatter in your computer. (You can use the command where in bash to get it)
    • Arguments: $FilePath$ -i -a -a In this case, we put 2 flags that refer to:
      • i : is the —in—place option
      • a: defines the level of aggressiveness with which we want the analysis to be carried out. How many more -a, more level of aggressiveness.
    • Working directory: $ProjectFileDir$

      external_tool_autopep8.png

      With this configuration, the tools could be used from the IDE from the menu Tools

      Tools-menu.png

  6. We would like the IDE to analyse and format automatically the files without us forcing it. So the next step would be defining the tools as File Watchers:
    • Click on the + symbol and press in (*A new window will be opened. The config below needs to be applied)*

      plus-file-watchers.png

    • To define the pycodestyle:
      • Name: your desired Name
      • File type: Python
      • Scope: Project Files
      • Program: the location of the formatter in your computer.
      • Arguments: $FilePath$
      • Output paths to refresh: $FilePath$
      • Working directory: $ProjectFileDir$

      watcher_pycodestyle.png

    • The same with pydocstyle and autopep8:

      watcher_autopep8.png

Once the watchers are configured, we can do two things:

  • Enable de autosave option.
    • If we enable it for pycodestyle, it will perform analysis automatically after saving.
    • If we enable it for autopep8, it will format the code automatically after saving.

      autosave.png

  • If on the contrary, we want to use a keyboard shortcut. We need to go to Preferences → Keymap. There, we will look for “External Tools” and add the keyboard shortcut that we want. In this way, simply with the shortcut that we have configured, it will format or analyze the code with the linter.

    add_as_a_short_cut.png

After this configuration, we have got all set.

Thanks for reading