Long-awaited PEP 8 checks on the fly, improved doctest support and more in PyCharm 2.7

PyCharm integration with PEP 8

It is not a secret that readability is one of the key features of Python. Indeed, a high level of readability is at the heart of Python’s design, following the recognized fact that code is read much more often than it is written. Developers always need to share their code, so it is very important for that code to be easily understood by others. And here come different coding style guides and standards. One of them, PEP 8, is the de facto coding style guide for Python.

PyCharm 2.7 takes care of readability and general beauty of your code. Now it supports continuous checking of your code for PEP 8 compliance on the fly, as you type it in the editor. PyCharm highlights any coding style violations and gives you suggestions, which you can apply instantly with the well known PyCharm quick-fixes:

Following PEP 8 is a good practice, although there are some situations where it is desirable to depart from the standard. The PyCharm team respects your freedom of choice, so you can disable specific PEP 8  warnings if you really need to. You can do that with the following quick-fix:


Or you can completely manage your inspections profile with Settings | Project Settings | Inspections:

Automatic imports optimization in PyCharm

Whenever you remove usages, it’s easy to forget to remove the corresponding imports. Now, in addition to standard PEP 8 checks, PyCharm is capable of optimizing imports. You no longer have to stop what you are doing, scroll to the top of the file, find the unused imports, and remove them manually.

This feature formats the existing import statements, reordering them according to PEP 8, and removes unused ones as well. All you need to do is press Ctrl+Alt+O or go to Code|Optimize imports…




PyCharm recognizes Python code in your doctests

There is a nice standard library module called doctests which lets you document your code and write tests for it simultaneously. Now PyCharm recognizes Python code in your doctests, and provides syntax highlighting and even code insight for it:

It correctly recognizes code in doctests even though it has different context. For example, when performing Optimize imports, PyCharm takes care not to remove imports in doctests in case they are required.

New general editing features in PyCharm

PyCharm 2.7 also includes two minor but neat features:

  • Edit | Fill Paragraph allows you to reflow the current string literal, comment or paragraph in a plain text file, so that lines wrap around and don’t exceed the right margin
  • Shadowing built-ins is a new inspection that highlights definitions of names that match the names of Python built-in functions:

Unwrap/Remove editing action in PyCharm

Now PyCharm supports the Unwrap/Remove action for Python, which provides a quick way to remove a control statement surrounding a block of code. To unwrap or remove a statement, place the caret on the expression and press Ctrl+Shift+Delete or go to Code | Unwrap/Remove.

PyCharm shows a pop-up window with all actions that are available in the current context:


We’ve also introduced several refactorings and code insight features that you may find interesting. See the What’s new page or these blog posts for more detailed descriptions: Refactoring enhancements in PyCharm 2.7 and Dynamic runtime type inference in PyCharm 2.7.

Develop with Pleasure!
PyCharm team

About Dmitry Filippov

Product Marketing Manager at JetBrains
This entry was posted in Cool Feature. Bookmark the permalink.

16 Responses to Long-awaited PEP 8 checks on the fly, improved doctest support and more in PyCharm 2.7

  1. istlota says:

    I admit to being an incorrigible PyCharm fan. Version 2.7 makes a great IDE even better. I absolutely love the new Darcula appearance. My last formal programming class was three decades ago, years before Python was a twinkle in Guido van Rossum’s Mind-Eye, even before I had written my first line of OOP code. Since then, open source software has been my Guru, with PyCharm being my Sat Guru for all things Python.

    PyCharm’s PEP8 inspection feature, in particular, has become my second most valuable tool [first being the I in I] for leading I from writing working Python code to writing elegant Python code.

    I love the new inspection feature for detecting ‘shadowed’ variables — yet another bad habit of mine PyCharm nudged me to break.

    All of Man’s suffering can be traced to his misconception of What is Real. Math is the language of the One, with it All that is Real may be modeled. As Above, So Below. Programming is a process by which mortals may transition between Math — pure and undefiled — and the Real World. PyCharm is among the best available environments for modeling the All.

  2. Kashif Ali says:

    I noticed that pep8 does show up in the code inspection tab. I had it set to ERROR so it under lines any pep8 errors in red, however once I did a inspection it didn’t show up in the tab but did highlight in the document. Is this how it is suppose to work ?

  3. Bhavani A B says:

    Thanks a lot for the informative post

  4. Pandu POLUAN says:

    Well, for the most part, I agree with PEP 8 conventions. However, I have my (consistent) coding style that keeps ‘conflicting’ with some PEP 8 conventions. So, I keep finding myself having to add “Ignores” to PEP 8.

    However, I keep finding myself in some cases where I want to specifically ignore PEP 8 within a file. Is it possible to add support for, let’s say, “# inspection_suppress_pep8 “?

  5. bigrob says:

    How can I disable the syntax highlighting in doctests? It makes it difficult to distinguish between doctests and the actual code…

  6. Daycoder says:

    I disabled a E251 a while back. I like whitespace. I’d argue PEP8 makes thinks more machine readable than human readable, but the consistency is needed to take advantage of refactoring. I’ve taken E251 out of the ignore errors list in both default setting and settings, but it no longer gets flagged.

    Also tried switching off PEP8 entirely, restarting PyCharm and re-enabling.

    Any ideas?

    • Dmitry Filippov says:

      Check Settings | Editor | Inspections | PEP8 coding style violator do you have anything in the Ignore errors section?
      If the problem still exists please file a ticket to support: jb.gg/pycharm-support

  7. Lance E Sloan says:

    When using the flake8 program, it reads settings from the “[flake8]” section of tox.ini. When using the pycodestyle program (FKA pep8), it reads settings from the “[pep8]” (some sources say “[pycodestyle]”) section of tox.ini. They are both places where one can put a “ignore=” setting with a list of error codes to be ignored by the respective programs.

    Since developers are already used to this convention, why doesn’t the Python plugin for IntelliJ IDEA follow it, too? I’m working with projects that have listed codes to be ignored in tox.ini, but IntelliJ IDEA still flags the violations in my code. I need to ignore each type of violation, one by one. What’s worse, IntelliJ IDEA doesn’t include the code when it gives the violation messages. So I end up copying the list of codes from tox.ini to the PEP8 inspection configuration in project settings.

    This is an annoying waste of time.

  8. Paulo César says:

    Sorry for my bad English, I’m still learning.
    My question, how do I identify multiple rows at the same time?
    Sometimes, refactor some function or class, the ID code too far from the margin. I wanted to be able to select and leave two border spaces.

Leave a Reply

Your email address will not be published. Required fields are marked *