Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint ungrouped-imports warning

Tags:

python

pylint

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?

like image 468
慕冬亮 Avatar asked Aug 31 '17 17:08

慕冬亮


2 Answers

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
like image 157
Nikhil Avatar answered Oct 16 '22 04:10

Nikhil


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.

like image 4
mikey Avatar answered Oct 16 '22 02:10

mikey