Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-import doesn't follow PEP8

Consider the following code:

from bs4 import BeautifulSoup


data = "<test>test text</test>"
soup = BeautifulSoup(data)
print(soup.find(text=re.compile(r'test$')))

It is missing an import re line and would fail with a NameError without it.

Now, I'm trying to use PyCharm's Auto-Import feature: focusing on re and hitting Alt+Enter, which opens up the following popup:

enter image description here

Now, if I choose Import 're' option, Pycharm would insert the new import line at the top of the script:

import re
from bs4 import BeautifulSoup


data = "<test>test text</test>"
soup = BeautifulSoup(data)
print(soup.find(text=re.compile(r'test$')))

Looks almost good, except that it doesn't follow PEP8 import guidelines:

Imports should be grouped in the following order:

  • standard library imports

  • related third party imports

  • local application/library specific imports

You should put a blank line between each group of imports.

In other words, there is a missing blank line between the two imports:

import re

from bs4 import BeautifulSoup

Question is: is it possible to tell Pycharm to follow the PEP8 guidelines and insert a new-line between the lines with different import types on auto-import?


As a workaround, I'm calling Optimize Imports after that organizes the imports correctly.

like image 701
alecxe Avatar asked Dec 08 '14 23:12

alecxe


People also ask

Does flake8 sort imports?

Project description A flake8 and Pylama plugin that checks the ordering of your imports. It does not check anything else about the imports. Merely that they are grouped and ordered correctly.

How do I fix Visual Studio import code?

While refactoring your javascript/typescript code it's easy to remove things but forget to get rid of the unused imports. In VSCode, you can easily remove these with the shortcut : Shift + Alt + O.

Does black sort import?

isort. isort helps to sort and format imports in your Python code. Black also formats imports, but in a different way from isort's defaults which leads to conflicting changes.

How do I order imports in Python?

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. Imports should be grouped in the following order: Standard library imports. Related third party imports.


1 Answers

You can't. Reason is PyCharm doesn't tell you that you have violated any PEP8 Guidelines if you do that or any import statements at all. One, your PyCharm is outdated (newest version is 4.0.2/4.2) or second, your PyCharm seems to be having a bug, thus giving reason to file a bug report. If you can try to safely download PyCharm again to try to fix the bug. If that doesn't work, file a bug report or make a habit of making a blank line between your statements. Hope this answers your question! Oh, it does not matter whether you use from, import, or both type of statements.

like image 149
Anthony Pham Avatar answered Oct 02 '22 12:10

Anthony Pham