Testing Python C libraries - get build path
Question: 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)


Answer: 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.

To get the current platform, use `sysconfig.get_platform()`. To get the python
version, use `sys.version_info` (specifically the first three elements of the
returned tuple). On my system (64-bit linux with python 2.7.2) I get:

    
    
    >>> import sysconfig
    >>> import sys
    >>> sysconfig.get_platform()
    linux-x86_64
    >>> sys.version_info[:3]
    (2, 7, 2)
    

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:

    
    
    def distutils_dir_name(dname):
        """Returns the name of a distutils build directory"""
        f = "{dirname}.{platform}-{version[0]}.{version[1]}"
        return f.format(dirname=dname,
                        platform=sysconfig.get_platform(),
                        version=sys.version_info)
    

You can use this to generate the name of any of distutils build directory:

    
    
    >>> import os
    >>> os.path.join('build', distutils_dir_name('lib'))
    build/lib.linux-x86_64-2.7
    

