Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Python C libraries - get build path

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)

like image 946
danodonovan Avatar asked Jan 14 '13 14:01

danodonovan


2 Answers

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
like image 105
SethMMorton Avatar answered Oct 28 '22 03:10

SethMMorton


I found that compiling the module in-place is best for testing purposes. To do so just use

python setup.py build_ext --inplace

This will compile all the auxiliary files in the temp directory as usual, but the final .so file will be put in the current directory.

like image 43
Dejan Jovanović Avatar answered Oct 28 '22 04:10

Dejan Jovanović