What is the best way of Importing a module to work on both Python 2 and Python 3?
Question:
I have two test launchers , one with python 2 env and another with python 3 env.
I use from itertools import izip_longest
which worked fine in python2 env. But the same module is missing in python3 env. Reason is izip_longest
was renamed to zip_longest
in Python 3.
To make the script work in both the env, I did something like below
Solution 1:
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
This worked as expected.
There is another way of handling this scenario.
Solution 2:
import six
if six.PY2:
from itertools import izip_longest as zip_longest
else
from itertools import zip_longest
This also worked as expected.
Question: Which is the best way of handling such differences between python 2 and python 3 ?
In solution 1, when the code is run on python 2, there is an import error which will be handled and then again script would import the correct module.
In solution 2, there is no such import error which we need to worry about handling it.
I have these two solutions.
Please suggest more efficient ones if any. Thanks.
Answers:
If you’re willing to use the six
module (and it seems like you are, since you used some of its basic functionality in one of your examples), you can use it a bit more and it will take care of all of this for you:
from six.moves import zip_longest
This will let you use the Python 3 name, zip_longest
, in both Python 2 and Python 3. Other renamed modules and functions work similarly, just import them (usually under their Python 3 name) from within the "fake" module six.moves
. Check out the docs for more details.
I have two test launchers , one with python 2 env and another with python 3 env.
I use from itertools import izip_longest
which worked fine in python2 env. But the same module is missing in python3 env. Reason is izip_longest
was renamed to zip_longest
in Python 3.
To make the script work in both the env, I did something like below
Solution 1:
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
This worked as expected.
There is another way of handling this scenario.
Solution 2:
import six
if six.PY2:
from itertools import izip_longest as zip_longest
else
from itertools import zip_longest
This also worked as expected.
Question: Which is the best way of handling such differences between python 2 and python 3 ?
In solution 1, when the code is run on python 2, there is an import error which will be handled and then again script would import the correct module.
In solution 2, there is no such import error which we need to worry about handling it.
I have these two solutions.
Please suggest more efficient ones if any. Thanks.
If you’re willing to use the six
module (and it seems like you are, since you used some of its basic functionality in one of your examples), you can use it a bit more and it will take care of all of this for you:
from six.moves import zip_longest
This will let you use the Python 3 name, zip_longest
, in both Python 2 and Python 3. Other renamed modules and functions work similarly, just import them (usually under their Python 3 name) from within the "fake" module six.moves
. Check out the docs for more details.