Name of the Python shared library, i.e the Python shared library file name (e.g libpython3.9m.dylib
, libpython3.7.so
) without the lib
prefix and the file extension.
The Python shared library take the form of python{ldversion}
where ldversion
is {major}.{minor}{abiflags}
. There are 2 ways to get the name of the Python shared library
from sysconfig import get_config_var
ldversion = get_config_var("LDVERSION")
python_native_lib = f"python{ldversion}"
or
from sys import abiflags
import sysconfig
version = sysconfig.get_python_version()
python_native_lib = f"python{version}{abiflags}"
We went with the 2nd method since it is more portable.
The directory where the Python shared library lives. It is located in either f"{sys.base_prefix}/lib"
or config dir sysconfig.get_config_var('LIBPL')
. For each Python installation, which one of these 2 directories actually contains the shared library is almost arbitrary. We decided to just use both with priority given to the config dir f"{sysconfig.get_config_var('LIBPL')}:{sys.base_prefix}/lib"
.
The path to the Python interpreter executable, used as the input to Py_SetProgramName
. The Python doc recommends running Py_SetProgramName
prior to Py_Initialize
so that the correct Python run-time libraries relative to the interpreter executable can be found (prefix
, exec_prefix
, ...)
https://docs.python.org/3/extending/embedding.html#very-high-level-embedding https://docs.python.org/3/c-api/init.html#c.Py_SetProgramName
This enables using ScalaPy with virtualenv. Since a Python installation inside virtualenv shares the same shared library (libpython...so
) with the base installation, without Py_SetProgramName
, there's no way to point ScalaPy towards the runtime directories set by virtualenv.
import sys
sys.executable
The recommended linker options for embedding the Python interpreter into another application, mostly extracted from the outputs of
pythonX.Y-config --ldflags
for python 3.7
and
pythonX.Y-config --ldflags --embed
for python 3.8+
,
with added library paths from nativeLibraryPaths
https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems
The full path to pythonX.Y-config
is f"{sys.base_prefix}/bin/python{ldversion}-config"
.