4 ms·
Hit an interesting issue when trying to make my urllib2 Python code behave the same way under both Python 2.7 and 3.6, which is fortunately documented: https://
by minimaxir 10y ago
Hit an interesting issue when trying to make my urllib2 Python code behave the same way under both Python 2.7 and 3.6, which is fortunately documented: https://docs.python.org/2/library/urllib2.html https://docs.python.org/2/library/urllib2.html
> Note: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
The best-but-silly solution I found without introducing a dependency was:
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
- orf 10y agoUse six for this. They provide stable import paths for stdlib modules that have moved. > from six.moves import urllib