Pylint ungrouped-imports warning - python

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.

Related

ImportError: No module named error when importing urllib.error

I am just getting my feet wet in the art of webscraping and I am following the tutorials from this source. For some reason I cannot import the error module from 'urllib' to handle exceptions. Since this is a built-in library, I am confused as to why this is an issue.
from urllib import urlopen
from urllib.error import HTTPError
from urllib.error import URLError
yields the error
ImportErrorTraceback (most recent call last)
<ipython-input-1-30b72b3bf2ea> in <module>()
1 from urllib import urlopen
----> 2 from urllib.error import HTTPError
3 from urllib.error import URLError
I have tried the same code with another IDE (IntelliJ) and it works as expected leading me to believe that this could be an issue with Google Colab itself. Could someone weight in and possibly help me find a solution to this problem.
I am new to programming, so if this is a juvenile question or if this is not the appropriate place for this question, I apologize in advance.
P.S. I have double checked that the runtime is Python 3
Your problem is in
from urllib import urlopen
Right way to import urlopen is from urllib.request
from urllib.request import urlopen
Docs
Just try this:
from urllib.request import urlopen
Always remember to try to search the docs of a particular library it helps a lot.
You are trying to run this code using Python 2. Use Python 3 and it will work.
Python 2:
>>> from urllib.error import HTTPError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named error
>>>
Python 3:
>>> from urllib.error import HTTPError
>>>

Pylinting a python 2 fallback

I have a python module with a python 2 fallback set up through try/catch.
try:
from urllib.parse import urlencode
except ImportError:
from urlib import urlencode
When I pylint the file I get no name 'urlencode' in module 'urllib' and similar errors. Is there anyway to specify python 2 linting for a block, disable all linting for a block, or am I stuck hand squelching all errors?
I settled on a nicer way of doing this by disabling the linting errors at the beginning of a python 2 block, and then re enabling them at the end.
# pylint: disable=no-name-in-module, import-error
from urllib import urlencode
from urllib2 import urlopen
# pylint: enable=no-name-in-module, import-error

Python: Importing urllib.quote

I would like to use urllib.quote(). But python (python3) is not finding the module.
Suppose, I have this line of code:
print(urllib.quote("châteu", safe=''))
How do I import urllib.quote?
import urllib or
import urllib.quote both give
AttributeError: 'module' object has no attribute 'quote'
What confuses me is that urllib.request is accessible via import urllib.request
In Python 3.x, you need to import urllib.parse.quote:
>>> import urllib.parse
>>> urllib.parse.quote("châteu", safe='')
'ch%C3%A2teu'
According to Python 2.x urllib module documentation:
NOTE
The urllib module has been split into parts and renamed in Python 3 to
urllib.request, urllib.parse, and urllib.error.
If you need to handle both Python 2.x and 3.x you can catch the exception and load the alternative.
try:
from urllib import quote # Python 2.X
except ImportError:
from urllib.parse import quote # Python 3+
You could also use the python compatibility wrapper six to handle this.
from six.moves.urllib.parse import quote
urllib went through some changes in Python3 and can now be imported from the parse submodule
>>> from urllib.parse import quote
>>> quote('"')
'%22'
This is how I handle this, without using exceptions.
import sys
if sys.version_info.major > 2: # Python 3 or later
from urllib.parse import quote
else: # Python 2
from urllib import quote
Use six:
from six.moves.urllib.parse import quote
six will simplify compatibility problems between Python 2 and Python 3, such as different import paths.

python urllib error

so I have this code:
def crawl(self, url):
data = urllib.request.urlopen(url)
print(data)
but then when I call the function, it returns
data = urllib.request.urlopen(url)
AttributeError: 'module' object has no attribute 'request'
what did I do wrong? I already imported urllib..
using python 3.1.3
In python3, urllib is a package with three modules request, response, and error for its respective purposes.
Whenever you had import urllib or import urllib2 in Python2.
Replace them with
import urllib.request
import urllib.response
import urllib.error
The classs and methods are same.
BTW, use 2to3 tool if you converting from python2 to python3.
urllib.request is a separate module; import it explicitly.

Importing a module based on installed python version?

My module currently imports the json module, which is only available in 2.6. I'd like to make a check against the python version to import simplejson, which can be built for 2.5 (and is the module adopted in 2.6 anyway). Something like:
if __version__ 2.5:
import simplejson as json
else:
import json
What's the best way to approach this?
try:
import simplejson as json
except ImportError:
import json
of course, it doesn't work around cases when in python-2.5 you don't have simplejson installed, the same as your example.
Though the ImportError approach (SilentGhost's answer) is definitely best for this example, anyone wanting to do that __version__ thing would use something like this:
import sys
if sys.version_info < (2, 6):
import simplejson as json
else:
import json
To be absolutely clear though, this is not the "best way" to do what you wanted... it's merely the correct way to do what you were trying to show with __version__.
You can import one or more modules without Handling ImportError error:
import sys
major_version = sys.version_info.major
if major_version == 2:
import SocketServer
import SimpleHTTPServer
import urllib2
elif major_version == 3:
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import urllib.request as urllib2

Categories

Resources