In my python script - youdao.py, in order to be compatible with python2 and python3, I import urlopen like this style:
try:
# compatible for python2
from urllib import urlencode
from urllib2 import urlopen
except ImportError:
# compatible for python3
from urllib.parse import urlencode
from urllib.request import urlopen
See details in https://github.com/MintCN/youdao-python/blob/master/youdao_simple/youdao.py#L22
When you use pylint youdao.py
, you will see ungrouped-imports warning
, how do I modify code to remove this warning?
I had similar issue. Pylint prefers grouping of packages.
CASE 1: Causes ungrouped-imports warning
import keras
import sklearn
from keras import losses
from sklearn import svm
CASE 2: [No Warning]
import keras
from keras import losses
import sklearn
from sklearn import svm
try:
# compatible for python2
# from urllib import urlencode
from urllib2 import urlopen
from urllib import urlencode
except ImportError:
# compatible for python3
from urllib.parse import urlencode
from urllib.request import urlopen
This fixes it -- all urllib imports should appear uninterrupted; otherwise pylint complains.
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