This question tells me how to check the version of Python. For my package I require at least Python 3.3:
MIN_VERSION_INFO = 3, 3
import sys
if not sys.version_info >= MIN_VERSION_INFO:
exit("Python {}.{}+ is required.".format(*MIN_VERSION_INFO))
but where/when should this check occur?
I want to produce the clearest possible error message for users installing via pip
(sdist and wheel) or python setup.py install
. Something like:
$ pip -V
pip x.x.x from ... (Python 3.2)
$ pip install MyPackage
Python 3.3+ is required.
$ python -V
Python 3.2
$ python setup.py install
Python 3.3+ is required.
The primary point of a compatibility check is to have this check either elegantly handle a compatibility issue or gracefully exit with an explanation before the incompatibility causes problems.
I'd put it near the top of
setup.py
, or the first script that belongs to you that will be called. Best practice is not to include code in__init__.py
(unless you're making a MAJOR package, I'd opine), but if you already have a lot of code there, it's fine.The quickest point of reference I can find: The old Python 2.6
unittest
module had a test for it near the top, naturally, after the module docstring, imports, and__all__
.Another point of reference shows a compatibility check in an
__init__.py
, again, near the top, though here it is immediately after the docstring and an import ofsys
, required for the check. There are other similar examples of this usage in__init__.py
in this same set of libraries.