@@ -2,13 +2,13 @@ Targeting Python 2+3
22--------------------
33
44In a lot of cases you might want to develop programs which can be run in
5- both, Python 2+ and 3+.
5+ both Python 2+ and 3+.
66
7- Just imagine that you have a very popular python module which is use by
8- hundreds of people but not all of them have python 2 or 3. In that case
7+ Just imagine that you have a very popular Python module which is use by
8+ hundreds of people but not all of them have Python 2 or 3. In that case
99you have two choices. The first one is to distribute 2 modules, one for
10- python 2 and the other for python 3. The other choice is to modify your
11- current code and make is compatible with both python 2 and 3.
10+ Python 2 and the other for Python 3. The other choice is to modify your
11+ current code and make is compatible with both Python 2 and 3.
1212
1313In this section I am going to highlight some of the tricks which you can
1414employ to make a script compatible with both of them.
@@ -19,7 +19,7 @@ The first and most important method is to use ``__future__`` imports. It
1919allows you to import Python 3 functionality in Python 2. Here is an
2020example:
2121
22- - Context manager were new in Python 3. For using them in Python 2.5+
22+ - Context managers were new in Python 2.6+. For using them in Python 2.5
2323 you can use:
2424
2525.. code :: python
@@ -57,28 +57,28 @@ Do you know that you can do something like this as well?
5757
5858 import foo as foo
5959
60- I know it’s function is the same as above listed code but it is vital
61- for making your script compatible with python 2 and 3. Now examine the
60+ I know its function is the same as the above listed code but it is vital
61+ for making your script compatible with Python 2 and 3. Now examine the
6262code below :
6363
6464.. code :: python
6565
6666 try :
67- import urllib.request as urllib_request # for python 3
67+ import urllib.request as urllib_request # for Python 3
6868 except ImportError :
69- import urllib2 as urllib_request # for python 2
69+ import urllib2 as urllib_request # for Python 2
7070
7171 So let me explain the above code a little. We are wrapping our importing
72- code in a try except clause. We are doing it because in python2 there is
73- no urllib.request module and will result in an ImportError. The
74- functionality of urllib.request is provided by urllib2 module in
75- python2 . So now when in Python2 we try to import ``urllib.request `` and
76- get an ``ImportError `` we tell Python to import urllib2 instead.
72+ code in a try except clause. We are doing it because in Python 2 there is
73+ no `` urllib.request `` module and will result in an ImportError. The
74+ functionality of `` urllib.request `` is provided by `` urllib2 `` module in
75+ Python 2 . So now when in Python 2 we try to import ``urllib.request `` and
76+ get an ``ImportError `` we tell Python to import `` urllib2 `` instead.
7777
7878The final thing you need to know about is the ``as `` keyword. It is
7979mapping the imported module to ``urllib_request ``. So that now all of
80- the Classes and methods of urllib2 are available to us by
81- urllib \_ request .
80+ the Classes and methods of `` urllib2 `` are available to us by
81+ `` urllib_request `` .
8282
8383**Obsolete Python 2 builtins **
8484
@@ -111,7 +111,6 @@ functionality in Python 2. For instance we have:
111111- singledispatch ``pip install singledispatch ``
112112- pathlib ``pip install pathlib ``
113113
114-
115- I am sure there are a lot of other methods and tricks which can be used
116- to make you code compatible with both of these Python series. This was
117- just to give you some ideas.
114+ For further reading, the Python documentation has a `comprehensive guide
115+ <https://docs.python.org/3/howto/pyporting.html> `_ of steps you need to
116+ take to make your code compatible with both Python 2 and 3.
0 commit comments