慕冬亮
慕冬亮

Reputation: 351

Pylint ungrouped-imports warning

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?

Upvotes: 12

Views: 21251

Answers (2)

mikey
mikey

Reputation: 2674

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.

Upvotes: 5

Nikhil
Nikhil

Reputation: 1172

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

Upvotes: 28

Related Questions