0

My current requirement is to write a script in Python 2.4.3 which comes bundled wit RHEL 5 . But few years down the line say 5 yrs ,the server may be upgraded to RHEL 8 or 9 which comes with Python 3. So I am supposed to write the code which is compatible with both version. Now my scripts contain basic operations such as creating directories, unzipping files, file manipulations, XML file reading ( which I am doing now with minidom ) and some hashing.

Now I found out that a tool called python-modernize does the work which is built on top of 2to3. I searched for the tool but I got a tar.gz file . It does not contain a python-modernize file as they say it in its usage. But all I found was a setup.py file. I am new to Python and all I know is that something is done with pip. But I also read that using pip is a tough work for Python 2.4.3 .

Please tell me how I can get the job done.

I also referred this but couldn't find how to use the tool.

Also please let me know if there are any good alternative to this.

Community
  • 1
  • 1
v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • 1
    See https://docs.python.org/3/howto/pyporting.html. This also recommends **moving to Python 2.7** - there are many more incompatibilities if you have to continue support for 2.4.3 (which isn't even the latest on the 2.4 branch; see https://www.python.org/download/releases/2.4.3/). – jonrsharpe May 13 '15 at 12:06
  • @jonrsharpe The RHEL in the server will not be updated until 5 yrs from now. I tried convincing the higher people to update Python, but in vain :| I tried asking them to install python 3 but they ask for a rpm file :( – v1shnu May 13 '15 at 12:20
  • 2
    See e.g. https://blog.serverdensity.com/updating-python-on-rhelcentos/ Alternatively, just give up, write working code now and worry about Py3 porting when (and if) the upgrade actually happens; 5 years is a **long** time away! – jonrsharpe May 13 '15 at 12:26
  • 2
    +1 zillion to @jonrsharpe on this - write the code with the constraints you've got. Five years is an eternity in IT years. There's a good chance you won't be using this script 5 years from now. :-D – Deacon May 13 '15 at 12:41
  • @jonrsharpe Nice read ! But the guys wont install any open source rpm's as well. Guess I have to go along with what I've got. – v1shnu May 13 '15 at 13:04
  • So the system contains non-OSS RPM(s) only? – James Mills May 13 '15 at 13:12
  • So, the rule is "no open source on Linux." Is that right? If so, it does not seem likely that you will convince "the guys" of anything. Write working code in 2.4 and let them pay the price when/if the time comes. Of course, you need to evaluate this opportunity in relationship to your personal career development. – lit May 25 '15 at 21:44

1 Answers1

1

Use the six libary from Benjamin Peterson.

It was specifically designed for this very use-case. It allows you to have a single codebase that supports both Python 2 and Python 3 by importing the things that are different from 2 to 3 from the six library.

Just simply import it and use it's definitions:

import six

Example:

from six import u, b

The above are classic where Python 3+ introduced stricter changes to the way Unicode Strings and Bytes work.

Six is a Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions. See the documentation for more information on what is provided.

Six supports every Python version since 2.5. It is contained in only one Python file, so it can be easily copied into your project. (The copyright and license notice must be retained.)

Update: Sorry I missed the Python<=2.5 requirement there; but honestly I think using six is your best option. YOu'll have to convince the powers that be that you have to use a minimum of Python 2.5.

Update #2: My strongest recommendation is to upgrade your code to Python 2.7 as a minimum already. Don't get left behind or you'll always be behind the ball!

Update #3: As per the above very helpful comments; I agree. Just work with what you've got right now. 5 years is indeed a long time and those same restrictions may not be in place by then!

James Mills
  • 18,669
  • 3
  • 49
  • 62
  • *"Six supports every Python version since 2.5"* The OP is targeting 2.4.3 as one of their versions. – Cory Kramer May 13 '15 at 12:00
  • @Cyber Good point; I'm not sure there is a *good* solution for Python < 2.5; Even circuits dropped support for 2.5 a few years ago :) *sigh* – James Mills May 13 '15 at 12:04
  • @JamesMills I tried convincing the server guys to upgrade but they will not until the next 5 years. Also tried asking them to install python 3 but they ask for a rpm file. Guess I don't have any option left. Any other options ? – v1shnu May 13 '15 at 12:23
  • @ViChU I'm afraid not really. That kind of attitude towards software systems is IHMO archaic, error prone and full of maintenance nightmares. I'm not sure of any options that will satisfy your needs :/ – James Mills May 13 '15 at 12:44