Did you try installing SWI-Prolog from SWI-Prolog downloads ?
Despite its name, the “development” version is very stable and it has some bug fixes that aren’t in the “stable” version.
SWI-Prolog 8.5.8 is pretty recent, which means you got it from the “devel” page and not from the “stable”. So, please ignore my comment about that. Possibly someone else can help you; I don’t know enough about MacOS or PySwip to help you. But, in my experience, rebuilding everything from sources can sometimes fix things (that is, rebuilding both SWI-Prolog and PySwip).
For the “pip install”, you might try instead: python3.8 -mpip install ....
The error message [FATAL ERROR: ...] doesn’t look like a typical Python error message.
I tried running PySwip on my ChromeBook (Debian), with the example at https://github.com/yuce/pyswip and it crashed when I input prolog.assertz("father(michael,john)").
(using Python 3.9)
But, in my experience, rebuilding everything from sources can sometimes fix things (that is, rebuilding both SWI-Prolog and PySwip).
Would this work even if i used the installer from the downloads page? Or would i have to delete my installation and build SWI (and Pyswip) from source?
I see this message once in a while when swipl does not find its home directory, so consider setting the environment variable SWI_HOME_DIR accordingly, before “from pyswipl import Prolog”.
The SWI-Prolog “home” is the directory containing boot.prc, which is the system resource file it cannot find. The organization of the MacOS app is a little different from other platforms to satisfy the bundle standards. The home is (with default system wide installation) /Applications/SWI-Prolog.app/Contents/swipl.
Note there are two ways to find this. One is by running the command below, which prints several variables in POSIX sh syntax that provide information one may need to embed Prolog or link foreign code that against Prolog. The home is in PLBASE
Quite unlikely because the message that it cannot find the system resources comes from this library. In the app bundle it is in Contents/Frameworks/libswipl.dylib.
Added export SWI_HOME_DIR=/Applications/SWI-Prolog.app/Contents/swipl to this file and saved it.
Now indeed when i do env in the Terminal i can see, among other things, SWI_HOME_DIR=/Applications/SWI-Prolog.app/Contents/swipl. So far so good.
But when i run from pyswip import Prolog i now get another error (below). Not sure but it seems to be a problem finding swipl? My swipl is here /Applications/SWI-Prolog.app/Contents/MacOS, which is another directory than the one containing boot.prc, which is in /Applications/SWI-Prolog.app/Contents/swipl
Traceback (most recent call last):
File "/Users/JCRohner/Documents/Jobb/PySwip/PySwipTest.py", line 1, in <module>
from pyswip import Prolog
File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/__init__.py", line 29, in <module>
from pyswip.prolog import Prolog
File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/prolog.py", line 28, in <module>
from pyswip.core import *
File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/core.py", line 567, in <module>
(_path, SWI_HOME_DIR) = _findSwipl()
File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/core.py", line 419, in _findSwipl
(path, swiHome) = _findSwiplMacOSHome()
File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/core.py", line 348, in _findSwiplMacOSHome
(path_res, back_path) = walk(path, name)
TypeError: cannot unpack non-iterable NoneType object
PS as you suggested libswipl was actually there and i eventually found it when i knew where to look; i was initially unable to find it when searching with Spotlight on mac.
$ python3.9 -mpip install pyswip --user
Collecting pyswip
Downloading pyswip-0.2.10-py2.py3-none-any.whl (27 kB)
Installing collected packages: pyswip
Successfully installed pyswip-0.2.10
$ python3.9
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyswip import Prolog
>>> prolog=Prolog()
>>> prolog.assertz("father(michael, john)")
[Thread 1 (main) at Tue Jun 21 09:44:35 2022] ../src/pl-fli.c:2556: PL_put_chars: Assertion failed: 0
C-stack trace labeled "assert_fail":
[0] save_backtrace() at /home/peter/src/swipl-devel/build/../src/os/pl-cstack.c:334 [0x7bc9b158166e]
[1] __assert_fail() at /home/peter/src/swipl-devel/build/../src/pl-assert.c:103 [0x7bc9b1524176]
[2] PL_put_chars() at /home/peter/src/swipl-devel/build/../src/pl-fli.c:2556 (discriminator 1) [0x7bc9b1552b24]
[3] ffi_prep_go_closure() at ??:? [0x7bc9b16d3d1d]
[4] ffi_closure_free() at ??:? [0x7bc9b16d3289]
[5] ??() at ??:0 [0x7bc9b1992360]
[6] ??() at ??:0 [0x7bc9b1991e05]
[7] python3.9(_PyObject_MakeTpCall+0x39b) [0x51d89b]
[8] python3.9(_PyEval_EvalFrameDefault+0x5654) [0x5170e4]
[9] python3.9(+0x14c00a) [0x54c00a]
[10] python3.9(+0x1c7290) [0x5c7290]
[11] python3.9(+0x1298c4) [0x5298c4]
[12] python3.9(_PyEval_EvalFrameDefault+0x525) [0x511fb5]
[13] python3.9(+0x1106ed) [0x5106ed]
[14] python3.9(_PyFunction_Vectorcall+0x361) [0x528d21]
[15] python3.9(+0x13bcfb) [0x53bcfb]
[16] python3.9(_PyEval_EvalFrameDefault+0x53e6) [0x516e76]
[17] python3.9(+0x1106ed) [0x5106ed]
[18] python3.9(_PyEval_EvalCodeWithName+0x47) [0x510497]
[19] python3.9(PyEval_EvalCode+0x23) [0x5f5be3]
[20] python3.9(+0x219de7) [0x619de7]
[21] python3.9(+0x215610) [0x615610]
[22] python3.9(+0x59cb3) [0x459cb3]
[23] python3.9(PyRun_InteractiveLoopFlags+0xd5) [0x459911]
[24] python3.9(PyRun_AnyFileExFlags+0x55) [0x6194f5]
[25] python3.9(+0x4bca9) [0x44bca9]
[26] python3.9(Py_BytesMain+0x29) [0x5ea6e9]
[27] __libc_start_main() at ./csu/../csu/libc-start.c:308 [0x7bc9b1df2d0a]
[28] python3.9(_start+0x2a) [0x5ea5ea]
Aborted (core dumped)
peter@penguin:~/src$
The crash occurs at the assert(0) in pl-fli.c … I’d need to rebuild things with the “-g” flag to get more information and I don’t have time to do that right now.
if ( flags == PL_ATOM )
w = textToAtom(&text);
else if ( flags == PL_STRING )
w = textToString(&text);
else if ( flags == PL_CODE_LIST || flags == PL_CHAR_LIST )
{ PL_put_variable(t);
rc = PL_unify_text(t, 0, &text, flags);
} else
assert(0);
Which version of SWI-Prolog are you using? The pyswip package is quite picky that it should be the stable version of SWI-Prolog (v8,4,3), and not the development version (8.5.12).
From looking at the pyswip code, it seems to use the ctypes module for foreign language calls; and the ABI is almost certainly different between swipl 8.4.3 and 8.5.12. That would account for the crashes.
Possibly, rebuilding pyswip in the context of swipl 8.5.12 would solve the problems, but I suspect that there are also some code changes needed. (If pyswip had used C code to interface to swipl, it would probably only require a recomple.)
Unfortunately. We should aim for maintaining binary compatibility, but that doesn’t always work It would also be better if users of the foreign interface relied on sources rather than hard coding constants from SWI-Prolog.h Anyway, there is a call PL_version_info() which can tell you the binary compatibility of libswipl using PL_VERSION_FLI. That way pyswip could at least tell the user this version is probably incompatible. It may of course not depend on the particular change …