What is a good way to support Python 2 in the Python 3 codebase when using PyPi?

I wrote a library in Python 3, and a port request has recently been added to this added Python 2.7 support. The stretch request is: https://github.com/JelteF/PyLaTeX/pull/9 .

Basically this is adding some import fixes and highlighting super calls. I have mixed feelings about this since one of the main reasons I chose for python 3 is the cleaner syntax, and that makes it compatible with the ugly syntax. However, I like that people stuck with Python 2 can also use the library.

This is why I was thinking of separate code files for python2 and python3. Is there a way to install PyPi so that it uses a separate codebase of different versions of Python? Using separate branches would be preferable, as merging new changes would be easy in this case.

Or is there some better option I'm missing?

+6
source share
3 answers

I just released my library for Python 2.7. What I did was use 3to2 and compile Python 3 code to version 2.7 before loading into PyPi.

 mkdir -p python2_source cp -R pylatex tests examples python2_source 3to2 python2_source -wn -f collections -f all 

You also need to add python2_source to MANIFEST.in , so it will be in your distributed files. Then I have this little piece of code in my setup.py to install from the python2_source folder if the setup is done using Python 2.7.

 if sys.version_info[0] == 3: source_dir = '.' else: source_dir = 'python2_source' 

and then package_dir={'': source_dir} in the actual setup call.

0
source

I would not put too much effort into supporting Python 2 in the Python 3 codebase. You are already using the β€œmodern” method - adding more complexity to your project to support those who are not updating should be low on your priorities.

Many projects written in Python 2 have been modified to work in Python 3 environments using 2to3 , but this is a group that helps solve those projects that are especially related to backward compatibility between major language versions. These projects work in an environment with the worst of the two worlds - they must program in Python 2, but ensure that their changes remain compatible with Python 3. The best practice, when possible, is to simply release all future updates in Python 3 .

In your case, you start with Python 3 and discuss support for the semantics of Python 2. This should not be a problem if you really don't care about Python 2 users. Even if you can get everything that works with the 3to2 tool, as you suggest, and even if you can trust that you are not introducing semantic problems *, you will now be hooked to continue to support and test this in the future, and you will be limited to improvements that remain backward compatible with Python 2. You have much better things that need to do with in PWM time - for example, with the improvement of your library - the support for such problems.

Instead, I suggest that you refuse this stretch request and ask the submitter to release its own clone. This separates your project from the Python 2 requirement, leaving you free to work on your project as needed, and letting the requestor who really cares about this requirement maintains it and supports it as they see fit.

* You claim that "this library does not actually use unicode and probably never will" - this is a dangerous way of thinking. Regardless of the use case, the lack of proper Unicode support will return to haunt you later. Do it right the first time and assume that failure to handle unicode will lead to unforeseen problems in the future - because it absolutely will.

+3
source

The easiest way to support Python 2 and Python 3 in the same code base is to use the six module to bridge the differences. However, this is still significant work, there are semantic differences between the two languages, especially if you are processing text.

+1
source

All Articles