Doron Behar
Doron Behar

Reputation: 2878

Python: Best way to get shared object library path from within a pypi package

I'm developing a Python package that includes an extension module:

# setup.py

from distutils.core import setup, Extension

setup(
    name="myPythonPkg",
    # ... all other args
    packages=["myPythonPkg"],
    ext_modules=[
        Extension('myFastCfunctions', ['myFastCfunctions.c'])
    ]
)

When I test the installation of this package with python setup.py install --prefix=$PWD/prefix I see (roughly):

<prefix>
└── lib
    └── python3.10
        └── site-packages
            ├── myFastCfunctions.cpython-310-x86_64-linux-gnu.so
            ├── myPythonPkg
            │   ├── __init__.py
            └── myPythonPkg-1.0.2-py3.10.egg-info

Inside myPythonPkg/__init__.py I'd like to get the path of myFastCfunctions.cpython-310-x86_64-linux-gnu.so and load it via ctypes.cdll.LoadLibrary. Of course I can paste that path directly, but I was wondering if there is a smarter, more platform and version agnostic way to doing that.

Upvotes: 0

Views: 116

Answers (1)

Doron Behar
Doron Behar

Reputation: 2878

Use EXTENSION_SUFFIXES[0], example:

from importlib.machinery import EXTENSION_SUFFIXES

myFastCfunctions_file = path.join(
    path.join(
        path.dirname(__file__), '..',
        'myFastCfunctions{}'.format(
            EXTENSION_SUFFIXES[0]
        )
    )
)

myFastCfunctions = cdll.LoadLibrary(myFastCfunctions_file)

Link to docs.

Upvotes: 0

Related Questions