When using setuptools/distutils to build C libraries in Python
$ python setup.py build
the *.so/*.pyd files are placed in build/lib.win32-2.7 (or equivalent).
I’d like to test these files in my test suite, but I’d rather not hard code the build/lib* path. Does anyone know how to pull this path from distutils so I can sys.path.append(build_path) – or is there an even better way to get hold of these files? (without having installed them first)
You must get the platform that you are running on and the version of python you are running on and then assemble the name yourself. This is an internal detail of setuptools (based on the bundled version of distutils), and so is not guaranteed to be stable.
To get the current platform, use
sysconfig.get_platform(). To get the python version, usesys.version_info(specifically the first three elements of the returned tuple). On my system (64-bit linux with python 2.7.2) I get:Before setuptools 62.1
The format of the lib directory is "lib.platform-versionmajor.versionminor" (i.e. only 2.7, not 2.7.2). You can construct this string using python’s string formatting methods:
After setuptools 62.1
The directory above clashes with alternative python implementations like PyPy, so the internal build directory was made more specific using
sys.implementation.cache_tag. The format of the lib directory is"lib.<platform>-<cachetag>". You can construct this string using python’s string formatting methods:You can use this to generate the name of any of distutils build directory:
Note that Setuptools 62.1 is only available on Python 3.7+. Also, if you set
SETUPTOOLS_USE_DISTUTILS=stdlibin your environment, then you will get the old behavior even in Setuptools 62.1.