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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With