Error "Could not find system resources" when trying to use PySwip

Hi!

I installed pyswip by doing

pip install git+https://github.com/yuce/pyswip@master#egg=pyswip --user

which worked.

But when i am trying to use pyswip by doing

from pyswip import Prolog

I get the following error:

[FATAL ERROR: at Sun Jun 19 12:17:14 2022
        Could not find system resources]

My python version: 3.8.2 64 - bit
My SWI version: 8.5.8, 64 - bit
OS: MacOs Catalina 10.15.7

In this path /Applications/SWI-Prolog.app/Contents/MacOS i have swipl

Maybe i am missing libswipl? Cannot find it anywhere when i search on my computer.

I installed SWI Prolog from here:
https://www.swi-prolog.org/download/stable

Cheers/JCR

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.

See also: Building SWI-Prolog on MacOSX

1 Like

Hi!

Yes i used the installer found on this page. Should i have built SWI with Homebrew or MacPorts instead? Would that make any difference?
Cheers/JCR

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)

1 Like

Thanks anyway…

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 tried:

python3 -mpip install git+https://github.com/yuce/pyswip@master#egg=pyswip --user

which worked. But the same error

[FATAL ERROR: at Sun Jun 19 12:17:14 2022
        Could not find system resources]

occurs when i run

from pyswip import Prolog

Cheers/JCR

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”.

1 Like

When i do env in a mac terminal i get (among other things)

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/SWI-Prolog.app/Contents/MacOS:$PATH:/Library/TeX/texbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands

This should mean that the path to swipl is known?

I also tried

putenv("SWI_HOME_DIR=/Applications/SWI-Prolog.app/Contents/MacOS/")

in the terminal but i got the error

zsh: unknown file attribute: _

Maybe the problem is that libswipl seems to be missing on my computer. Can’t find it anywhere and i think it is a requirement for PySwip

/JCR

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

swipl --dump-runtime-variables

The other is to start Prolog and run

?- current_prolog_flag(home, Home).

The syntax is (AFAIK, I do not use zsh)

export SWI_HOME_DIR=/Applications/SWI-Prolog.app/Contents/swipl

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.

1 Like

Thanks @Jan!

It seems i am getting closer. What i did:

  1. Opened Terminal (zsh)
  2. Typed nano ~/.zshrc
  3. 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.

Got a bit further. I manually edited core.py on line 380 to add the path for libswipl.dylib:

paths = ['.', './lib', '/usr/lib/', '/usr/local/lib', '/opt/local/lib', '/Applications/SWI-Prolog.app/Contents/Frameworks']

This

from pyswip import Prolog
prolog = Prolog()

now works which is an improvement from before. But doing

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")

Results in the error below (at least a Prolog error, yay! :upside_down_face:)

This error is also mentioned here, but with no replies
https://github.com/yuce/pyswip/issues/84

ERROR: atom_chars/2: Arguments are not sufficiently instantiated
Traceback (most recent call last):
  File "/Users/JCRohner/Documents/Jobb/PySwip/PySwipTest.py", line 3, in <module>
    prolog.assertz("father(michael,john)")
  File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/prolog.py", line 152, in assertz
    next(cls.query(assertion.join(["assertz((", "))."]), catcherrors=catcherrors))
  File "/Users/JCRohner/Library/Python/3.8/lib/python/site-packages/pyswip/prolog.py", line 128, in __call__
    raise PrologError("".join(["Caused by: '", query, "'. ",
pyswip.prolog.PrologError: Caused by: 'assertz((father(michael,john))).'. Returned: 'error(instantiation_error, context(:(system, /(atom_chars, 2)), _7106))'.

I get a different error on my ChromeBook (debian), which seems to be the same as Core dump · Issue #143 · yuce/pyswip · GitHub :

$ 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).

(I had huge problems when using the development version of SWI-Prolog trying to run Popper, which use pyswip. See this issue for the errors I got: Crash using v1.0.2 (problem with swi-prolog?) · Issue #28 · logic-and-learning-lab/Popper · GitHub ; though it’s not the same errors as you got.

Andrew Cropper wrote an pyswip issue on this: Error with SWI-Prolog v8.5.3 and pyswipl 0.2.10 · Issue #136 · yuce/pyswip · GitHub but no one has commented it.
After downgrading to the stable SWI-Prolog version everything works without any problem.
)

2 Likes

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.)

1 Like

Aha, now i remember! Thanks. I’ll try this out.
Cheers/JCR

TADA :slight_smile: Now it works. I should have read that Pyswip needs 8.4.3!

1 Like

Unfortunately. We should aim for maintaining binary compatibility, but that doesn’t always work :frowning: It would also be better if users of the foreign interface relied on sources rather than hard coding constants from SWI-Prolog.h :frowning: 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 …

2 Likes