Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neat way of making urllib work with python 2 and 3

I'm looking for suggestions on how to combine the two code snippets so that they work with both python 2 and 3. The goal is to make it "neat", ideally keeping it to one line and limiting any if/else/try/except constructs.

For python 3.x

   import xml.etree.ElementTree as ET, urllib.request, gzip, io
   url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
   oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.request.urlopen(url).read())))

For python 2.x

  import xml.etree.ElementTree as ET, urllib, gzip, io
  url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
  oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.urlopen(url).read())))
like image 799
hanno Avatar asked Jun 25 '14 19:06

hanno


People also ask

Are Python 2 and 3 compatible with each other?

Python 3 is not backward compatible with Python 2. Python 2 was mostly used to become a DevOps Engineer. It is no longer in use after 2020. Python 3 is used in a lot of fields like Software Engineering, Data Science, etc.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.

Does urllib2 work in Python 3?

In order to fetch an URL from the internet and use the data within the URL, the urllib2 standard python module was being used in Python2. The urllib2 module supported a lot of functions and classes that helped the users to open an URL and extract the contents. However, In Python3, the urllib2 module is not available.


2 Answers

If you don't want an extra dependency, you could simply use a try except block to import either module under the same alias...:

try:
    import urllib.request as urlrequest
except ImportError:
    import urllib as urlrequest

url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urlrequest.urlopen(url).read())))
like image 160
mgilson Avatar answered Sep 17 '22 14:09

mgilson


This is exactly what six was created for. It's a library designed to allow your code to work with both Python 2 and 3. (Don't let "library" scare you, it's intentionally just a single .py file to make it very easy to integrate/package.)

Instead of using the built-in urllib module, you'd use six's version which automatically redirects to the built-in module in both Python 2 and 3.

Here's what your code would look like:

import xml.etree.ElementTree as ET, gzip, io
from six.moves.urllib.request import urlopen
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urlopen(url).read())))

See: https://six.readthedocs.io/#module-six.moves.urllib.request

like image 37
Jon-Eric Avatar answered Sep 18 '22 14:09

Jon-Eric