Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active May 31, 2024 20:15
Show Gist options
  • Save wallentx/a9e816e565f3d1be77d3cd263ffd42b7 to your computer and use it in GitHub Desktop.
Save wallentx/a9e816e565f3d1be77d3cd263ffd42b7 to your computer and use it in GitHub Desktop.
Generates pyo3_config.txt
#!/usr/bin/env python
import sys
import os
import configparser
import sysconfig
# Function to detect the library directory
def detect_lib_dir():
libdir = sysconfig.get_config_var('LIBDIR')
if libdir and os.path.exists(libdir):
return libdir
return os.path.dirname(sys.executable)
# Function to detect the library name
def detect_lib_name():
libname = sysconfig.get_config_var('LDLIBRARY')
if libname:
# Strip the 'lib' prefix and the extension
return os.path.splitext(libname)[0][3:]
return 'python' + sys.version[:3].replace('.', '')
# Extract major.minor version
def get_major_minor_version():
return '.'.join(sys.version.split(' ')[0].split('.')[:2])
# Create a template configuration
template = {
'implementation': 'CPython',
'version': get_major_minor_version(), # Detecting major.minor Python version
'shared': 'true', # Assuming shared libraries are used
'abi3': 'true', # Assuming ABI3 is required
'build_flags': 'WITH_THREAD', # Assuming WITH_THREAD is needed
'suppress_build_script_link_lines': 'false',
'lib_dir': detect_lib_dir(), # Detecting library directory using sysconfig
'lib_name': detect_lib_name() # Detecting library name using sysconfig
}
# Write the configuration to the file
config_file = 'pyo3_config.txt'
with open(config_file, 'w') as configfile:
for key, value in template.items():
configfile.write(f'{key}={value}\n')
print(f"Configuration written to {config_file}")
@wallentx
Copy link
Author

Example output on termux:

implementation=CPython
version=3.11
shared=true
abi3=true
build_flags=WITH_THREAD
suppress_build_script_link_lines=false
lib_dir=/data/data/com.termux/files/usr/lib
lib_name=python3.11

From my server:

implementation=CPython
version=3.12
shared=true
abi3=true
build_flags=WITH_THREAD
suppress_build_script_link_lines=false
lib_dir=/usr/lib
lib_name=python3.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment