Gary Hoppenworth
Gary Hoppenworth

Reputation: 133

How do I make sure my Python 3.6 code is compatible with other minor versions?

If I'm writing a python package in python 3.6, then how do I ensure my code can be downloaded and ran in other python 3 environments, like python 3.5?

What makes some python packages (e.g. Tensorflow) compatible with all python 3 minor versions, while other python packages (e.g. OpenAI Gym) only compatible with Python 3.5?

Finally: if my goal is to write code that is compatible for Python 3.5 and 3.6, then would it be better to just use a python 3.5 environment?

Upvotes: 3

Views: 1299

Answers (1)

Kevin J. Chase
Kevin J. Chase

Reputation: 3956

The glib-but-true answer: Test your 3.6 code with those other versions of Python. If you want to see if something runs correctly in environment X, there's no substitute for actually running it in environment X.

Tox is a Python testing framework designed to do exactly this on your workstation. Something like this is often part of a larger continuous integration framework or service, which might be hosted on a remote server.

Writing your code in the earliest syntax you need to support is always a good idea, but it's not enough. You still need to test later versions, because functions or classes can change, and even bug-fixes can break your code if you were unwittingly depending on their behavior.


As for why some packages don't work under a specific minor version, the most likely reason is that they use a Python language feature that was introduced later. (Possibly it's one of their dependencies that requires the language feature.) This can include language syntax changes like Python 3.5's @ matrix-multiplication operator, all the way down to seeming trivia like Python 3.1's printing commas as thousands-separators, which is still enough to raise an exception.

It's actually a bit more complicated than just "supports version x or greater", because there are some gaps in Python's history. The most likely headache is the u'' Unicode literal syntax from Python 2. It was removed in Python 3.0... and then restored in Python 3.3, after its absence caused more grief than expected. That one change means any script with a u'Unicode literal' could work under Python 2.7 and 3.3+ while not working under Python 3.0, 3.1, or 3.2.

The Python documentation is very good about keeping track of when a feature was introduced. For instance, the first thing the typing module tells you is:

26.1. typing - Support for type hints

New in version 3.5.

A Ctrl-F style search for "new in" or "changed in" will generally turn up all the most important changes.

Upvotes: 4

Related Questions