Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the appropriate way of raising an error for having the wrong Python version?

Tags:

My code requires Python version of 3.6 or higher. To ensure this, I use the following:

import sys
version = sys.version_info
assert version.major > 3 or (version.major == 3 and version.minor >= 6)

But this doesn't seem like the best way to do this (from a good coding practices viewpoint). Is there a better way?

What is the appropriate way to make sure your script is being run on an appropriate version of Python?

like image 841
XYZT Avatar asked Mar 08 '18 19:03

XYZT


1 Answers

This information should be specified in your packaging information, so users are informed of the python version requirement before they ever install your python package.

Your project should have a setup.py script that is used to install your package. You can specify the python dependency in that setup script

setup(
    name='my_package',
    version='1.0.0',
    python_requires='>=3.6.0'
    ...
)

This way, it won't allow anyone to build, download, or install your package to an incompatible python version.

like image 191
Brendan Abel Avatar answered Sep 20 '22 12:09

Brendan Abel