- Python wrapper for the ScienceMode 4 protocol
- sciencemode_cffi is a Python wrapper for the ScienceMode library.
- This Python library has been tested on Windows 64-bit with Anaconda Python and on Linux.
- It uses cffi for Python bindings.
This package integrates the SMPT C library directly into the Python package:
- The SMPT C library is built as a static library using CMake
- The static library file (libsmpt.a, libsmpt.lib, etc.) is copied into the Python package directory
- When importing the module, the library is automatically handled via CFFI
- This eliminates the need for system-wide installation of the library
- CMake (>= 3.10)
- C compiler (GCC, Clang, MSVC, etc.)
- Python 3.x with pip
- CFFI (>= 1.0.0)
- pycparser (>= 2.14)
You can use the improved setup.py directly, which offers more options:
# Build just the C library
python setup.py build_lib
# Build with a specific build type
python setup.py build_lib --build-type=Debug
# Install the package with the built library
python setup.py installAlternatively, you can build the library directly with CMake:
# Create build directory and build the library
mkdir -p build_temp && cd build_temp
cmake ../smpt -DCMAKE_INSTALL_PREFIX=../ -DBUILD_SHARED_LIBS=OFF -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../lib
cmake --build .
# Copy the library to the Python package directory
cd ..
cp -f lib/libsmpt.a sciencemode/
# Install the Python package
pip install -e .If you prefer to use a pre-built library instead of building with CMake:
- Install MinGW from MinGW releases
- Adjust path where MinGW is installed in sciencemode/_cffi.py in line 202:
mingw_path = os.getenv('MINGW_PATH', default='...')
- Adjust path where MinGW is installed in sciencemode/_cffi.py in line 202:
- Install Visual Studio compiler
- Download smpt_windows_static_x86.zip from ScienceMode4_c_library
- Copy the static library (libsmpt.lib) from the archive to both:
- the /lib directory
- the /sciencemode directory (to bundle it with the Python package)
The package includes pytest tests to verify library loading and linking without requiring hardware. Basic tests are run automatically during installation, but you can run more comprehensive tests manually:
# Run the basic test script
python tests/run_unittest.py
# Run all tests with pytest
PYTHONPATH=. pytest tests -vThis script performs the essential tests to verify:
- The library can be imported
- The FFI interface is available
- Device structures can be created
- Library files are present in the package directory
The tests verify:
- That the library can be imported
- That basic structures and functions are accessible
- That FFI bindings work correctly
- That API functions can be called (using mock implementations)
These tests don't require physical hardware and provide confidence that the library is correctly built, linked, and functioning.
If you encounter issues with the installation:
- Make sure CMake is installed and in your PATH
- Ensure you have a working C compiler
- Try building with different options:
python setup.py build_lib --build-type=Debug - Check the lib directory to confirm the SMPT library was built properly
- Run the tests manually to check for library loading issues
- Look for detailed error messages in the build output
If you encounter issues with the static library, try these solutions:
Option 1: Copy the static library to your Python package directory:
# For Linux/macOS
cp lib/libsmpt.a sciencemode/
# For Windows
cp lib\libsmpt.lib sciencemode\
# Then reinstall
pip install -e .Option 2: Verify include paths and C compiler setup: Check that the include paths in _cffi.py point to the correct header files and that your C compiler is properly configured.
# Check that header files are available
ls -la include/ScienceMode4If you want to create a distributable wheel package that includes the static library:
# Prerequisites
pip install setuptools wheel
# Build the C library first
python setup.py build_lib
# Build the wheel
python setup.py bdist_wheel
# Install the wheel
pip install dist/*.whlThe improved setup.py supports several build options:
# Specify build type
python setup.py build_lib --build-type=Release # Options: Debug, Release, RelWithDebInfo, MinSizeRel
# The built library automatically uses multiple CPU cores for parallel building- The setup process will attempt to detect MSVC or MinGW compilers
- Static library files (.lib) will be copied to both lib/ and sciencemode/ directories
- Both 32-bit and 64-bit builds are supported
- Static library files (.a) will be copied to both lib/ and sciencemode/ directories
- The setup will search for libraries in standard installation directories
- GCC or Clang compilers are supported
- Static library files (.a) are used for all builds
- The setup handles macOS-specific build requirements