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

**URL:** <https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517>\
**Category:** Help!\
**Created:** [June 19, 2022, 10:24am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517 "2022-06-19T10:24:45Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 19, 2022, 10:24am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/1 "2022-06-19T10:24:45Z")

</div>

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:

```prolog
[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

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 19, 2022, 5:45pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/2 "2022-06-19T17:45:27Z")

</div>

Did you try installing SWI-Prolog from [SWI-Prolog downloads](https://www.swi-prolog.org/download/devel) ?  
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](https://www.swi-prolog.org/build/macos.html)

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 20, 2022, 3:24am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/3 "2022-06-20T03:24:49Z")

</div>

Hi!

> [@peter.ludemann](#):
>
> Did you try installing [SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual) from [SWI-Prolog downloads](https://www.swi-prolog.org/download/devel) ?

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

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 20, 2022, 6:56am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/4 "2022-06-20T06:56:03Z")

</div>

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](https://github.com/yuce/pyswip) and it crashed when I input `prolog.assertz("father(michael,john)")`.  
(using Python 3.9)

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 20, 2022, 10:12am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/5 "2022-06-20T10:12:07Z")

</div>

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

```prolog
[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

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [June 20, 2022, 12:59pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/6 "2022-06-20T12:59:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 21, 2022, 6:28am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/7 "2022-06-21T06:28:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 21, 2022, 7:16am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/8 "2022-06-21T07:16:49Z")

</div>

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

```

> [@JCR](#):
>
> in the terminal but i got the error

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

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

```

> [@JCR](#):
>
> libswipl seems to be missing on my computer

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

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 21, 2022, 3:25pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/9 "2022-06-21T15:25:01Z")

</div>

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`

```prolog
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.

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 21, 2022, 4:21pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/10 "2022-06-21T16:21:25Z")

</div>

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

```prolog
from pyswip import Prolog
prolog = Prolog()

```

now works which is an improvement from before. But doing

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

```

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

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

```prolog
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))'.

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 21, 2022, 4:52pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/11 "2022-06-21T16:52:17Z")

</div>

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

```prolog
$ 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.

```prolog
  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);

```

---

<div class="post-metadata">

**Author:** ![hakank](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hakank/32/44_2.png) [@hakank](https://swi-prolog.discourse.group/u/hakank)\
**Post date:** [June 21, 2022, 5:19pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/12 "2022-06-21T17:19:54Z")

</div>

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](https://github.com/logic-and-learning-lab/Popper/issues/28#issuecomment-995588199) ; 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](https://github.com/yuce/pyswip/issues/136) but no one has commented it.  
After downgrading to the stable SWI-Prolog version everything works without any problem.  
)

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 21, 2022, 7:20pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/13 "2022-06-21T19:20:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 21, 2022, 7:30pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/14 "2022-06-21T19:30:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [June 21, 2022, 7:35pm UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/15 "2022-06-21T19:35:56Z")

</div>

> [@hakank](#):
>
> 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).

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

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 22, 2022, 9:11am UTC](https://swi-prolog.discourse.group/t/error-could-not-find-system-resources-when-trying-to-use-pyswip/5517/16 "2022-06-22T09:11:20Z")

</div>

> [@peter.ludemann](#):
>
> 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.

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()](https://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_version_info%27)) 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 …
