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:
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.
import foo
belongs to. – PredictOptimize imports
in this case. This is something about auto-import here. Thanks. – Hecht