Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a python module is intended to be python 2 or python 3?

Python 2 and Python 3 have subtle differences which mean that it is not possible to look at a python module and certainly know, just from automatic code analysis, if it will work identically on python 2 and python 3. (Right? That seems to be the answer to Is it possible to check if python sourcecode was written only for one version (python 2 or python 3) )

Therefore, I suppose there must be some convention by which a developer can annotate a file to explicitly indicate that it is intended to be compatible with Python 2, Python 3, or both, so that this annotation can be read by developers, checked automatically, etc..

What is this convention?

I don’t see different file extensions, like .py2 vs .py3. I don’t see any global variable declaration intended to act as metadata. But it seems like something must exist, beyond ad hoc comments in code and readme files. So what is it?

like image 652
algal Avatar asked Jan 21 '18 02:01

algal


1 Answers

Unfortunately, there is not really any official way to specify it. However, a common way that's widely used is to specify the required Python version in the distribution's metadata.

You may see a line in the setup.py file (or the setup.cfg file, for modern versions of setuptools) declaring the python_requires option using the PEP440 syntax. See also PEP 345 - Metadata for Python Software Packages, specifically the section about environment markers and Requires-Python metadata. Using these markers will prevent pip from downloading/installing a distributions with an incorrect Python interpreter version.

For older packages, it's usually just mentioned in the docs or README file, or using trove classifiers. This is often listed on the PyPI and/or github landing page.

like image 103
wim Avatar answered Nov 12 '22 00:11

wim