Skip to content

“initialization/1 failed for: user:run_all_demos” on Apple Silicon macOS #1

@sporeking

Description

@sporeking

Hello,

Thank you for your excellent work!

I am experiencing a persistent issue where the Python library fails to initialize on an Apple Silicon Mac when using a Python environment
managed by Conda.

When I try to run the basic example (scryer-prolog examples/python_demo.pl), it fails with the warning:

❯ scryer-prolog examples/python_demo.pl                                 
=== Basic Python Demo ===
% Warning: initialization/1 failed for: user:run_all_demos
?- 

No Python code is executed.

Scryer-prolog version:

❯ scryer-prolog -v    
cargo:0.10.0

Python version: 3.12.12. I tried to use python@3.12 provided by brew and python@3.12 in miniconda env.

MacOS version: macOS Sequoia 15.3 arm64

I have conducted an extensive debugging process Maybe the root cause is a low-level incompatibility between Scryer Prolog's FFI and the FFI function signatures defined in src/lib/python.pl on this specific platform.

Key Debugging Steps and Findings (Most given by Gemini-cli. I'm not familiar with the implementation).:

  1. I confirmed that the libpython3.12.dylib from the Conda environment exists and that its architecture (arm64) correctly matches the Scryer Prolog
    executable's architecture (arm64).
  2. Setting the PYTHONHOME environment variable did not resolve the issue.
  3. I created a minimal Prolog test that bypassed the library and called use_foreign_module directly with only Py_Initialize and Py_Finalize. This test succeeded, proving that basic FFI calls and Python initialization are possible.
  4. Crucially, when I added the signature 'PyRun_SimpleString'([cstr], int) to the minimal test, the use_foreign_module call itself failed immediately.
  5. Using nm, I verified that the _PyRun_SimpleString C symbol is correctly exported from the Conda libpython3.12.dylib.
  6. I attempted a workaround by commenting out the PyRun_SimpleString signature in src/lib/python.pl and rewriting the predicate that uses it to rely on PyRun_String instead. This did not solve the problem, suggesting that other function signatures in the FFI list are also likely incompatible.

Gemini's Conclusion:
The evidence strongly suggests that one or more of the FFI function signatures in load_python_library_once are incorrect for the arm64 macOS platform, at least when linking against a Conda-provided libpython. The failure of use_foreign_module points to a direct signature mismatch, possibly related to C type sizes or alignment (int, long, i64, ptr) on this platform.

Can you tell me how to solve this or what should I do? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions