Python convention for specifying compatible interpreter versions?

Similar to the variables of the top-level module __author__ or __version__ , is there any convention for specifying supported versions of Python for the Python source file?

My usecase is a project with several scripts that should be compatible with Python 2.4. I would like to note this fact in them in some generally recognized way.

I am not asking how to require a minimal version of Python at runtime . My problem is that developers may accidentally use a function that is incompatible with the version of python that a particular script should support. PyCharm may warn of Python incompatibilities. It would be great if he could raise this annotation and set up a warning for each file.

+5
source share
1 answer

There is no convention that I know of for specifying supported versions of Python in the Python source file. If you create a Python library and distribute it through PyPI (and pip ), you can add the package metadata, which indicates the versions of Python with which it is compatible.

For example, for my scandir, you can see on PyPI that it is (currently) marked as compatible with Python 2.6, 2.7, and 3.2-3.5. This metadata is in the classifiers key in the setup.py package:

 setup( name='scandir', # ... classifiers=[ 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', # ... ] ) 

If you do not release this as a PyPI package, one of the alternatives may be triggered by a warning when a module is imported on an earlier version of Python, stating that the FizzBuzz function is not supported in this version of Python.

 import sys, warnings if sys.version_info < (3, 5): warnings.warn('The FizzBuzz feature is only supported on Python 3.5+') 

All that has been said, personally, I am just simply simple: the document that the function X is only supported on Python 3.5+, and then if someone tries to use this function in an older version, just let it fail.

+2
source

Source: https://habr.com/ru/post/1259934/


All Articles