# Erreur "assertion fail" lors de l'appel de pyswip en python

**URL:** <https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871>\
**Category:** General\
**Created:** [October 5, 2023, 6:01pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871 "2023-10-05T18:01:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [October 5, 2023, 6:01pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/1 "2023-10-05T18:01:19Z")

</div>

venant d"installer pyswip et ayant fait import pyswip  
j’ai l’erreur  
Assertion failed: 0, file /home/swipl/src/swipl-devel/src/pl-fli.c, line 2637

from pyswip import Prolog  
print(“bbb”)  
prolog = Prolog()  
print(“ccc”)  
for solution in prolog.query(“mortel(X)”):  
print("solution ",solution)  
print(“ddd”)  
solution et ddd ne s’affichent pas

---

<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:** [October 5, 2023, 6:34pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/2 "2023-10-05T18:34:01Z")

</div>

Replicated with SWI-Prolog version 9.1.16 for x86\_64-linux  
Every query I tried got a similar crash (e.g., goal `assert(mortel(foo))`, `[solution for solution in prolog.query("current_prolog_flag(X), writeln(X)")]`)  
The problem seems to happen when printing the term.

```txt
>>> for solution in prolog.query("mortel(X)"):
... print("solution ", solution)
... 
[Thread 1 (main) at Thu Oct 5 11:26:23 2023] ../src/pl-fli.c:2665: 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:337 [0x7c54ba3ad3e4]
  [1] __assert_fail() at /home/peter/src/swipl-devel/build/../src/pl-assert.c:105 [0x7c54ba375fe6]
  [2] PL_put_chars() at /home/peter/src/swipl-devel/build/../src/pl-fli.c:2667 [0x7c54ba391884]
  [3] ffi_prep_go_closure() at ??:? [0x7c54ba51fd1d]
  [4] ffi_closure_free() at ??:? [0x7c54ba51f289]
  [5] _call_function_pointer() at /home/peter/src/cpython/Modules/_ctypes/callproc.c:938 [0x7c54ba54b7e6]
  [6] PyCFuncPtr_call() at /home/peter/src/cpython/Modules/_ctypes/_ctypes.c:4201 [0x7c54ba546d78]
  [7] python3(_PyObject_MakeTpCall+0x6b) [0x50254b]
  [8] python3(_PyEval_EvalFrameDefault+0x6b4) [0x55bf94]
  [9] python3(+0x108867) [0x508867]
  [10] python3(_PyEval_EvalFrameDefault+0x4cb) [0x55bdab]
  [11] python3(+0x15aa7b) [0x55aa7b]
  [12] python3(PyEval_EvalCode+0x9a) [0x5d2e5a]
  [13] python3(+0x1e7113) [0x5e7113]
  [14] python3(+0x1e709a) [0x5e709a]
  [15] python3(+0xae73f) [0x4ae73f]
  [16] python3(_PyRun_InteractiveLoopObject+0xd7) [0x4aea9a]
  [17] python3(+0xacd48) [0x4acd48]
  [18] python3(PyRun_AnyFileExFlags+0x49) [0x4aeb99]
  [19] python3(+0xb841d) [0x4b841d]
  [20] python3(Py_BytesMain+0x27) [0x5ee957]
  [21] __libc_start_main() at ./csu/../csu/libc-start.c:308 [0x7c54badbdd0a]
  [22] python3(_start+0x2a) [0x58f2ca]
Prolog stack:
(null)
Aborted (core dumped)

```

---

<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:** [October 5, 2023, 7:25pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/3 "2023-10-05T19:25:11Z")

</div>

Correction: evaluating the generator from `Prolog.query()` crashed:

```python
for i in prolog.query("current_prolog_flag(X,Y)"): pass

```

---

<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:** [October 6, 2023, 7:20am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/4 "2023-10-06T07:20:18Z")

</div>

I _think_ it is a problem with the PyPi package. Using the git version from [GitHub - yuce/pyswip: PySwip is a Python - SWI-Prolog bridge enabling to query SWI-Prolog in your Python programs. It features an (incomplete) SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.](https://github.com/yuce/pyswip), this works fine for me.

pyswip uses Python ctypes. This duplicates the declarations from SWI-Prolog.h in the Python package. With SWI-Prolog, we are keen to keep C sources compatible and to a lesser extend maintain binary compatibility. Since a few years we provide better binary compatibility and a better interface to deal with incompatibilities. There are surprisingly old versions of SWI-Prolog floating around ☹

You can also install the latest SWI-Prolog development release (9.1.16) and either use Python from Prolog (easiest) or install the Janus Python package as described in [SWI-Prolog -- Janus as a Python package](https://www.swi-prolog.org/pldoc/man?section=janus-python-package). Janus provides a richer and faster interface between Prolog and Python.

---

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [October 6, 2023, 10:08am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/5 "2023-10-06T10:08:53Z")

</div>

he same happens with new swu prolog 9.1.16

---

<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:** [October 6, 2023, 10:32am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/6 "2023-10-06T10:32:40Z")

</div>

Yes, because pyswip on PyPi is old and incompatible with the current SWI-Prolog binaries. Either install pyswip from Github or switch to the (now) bundled Janus interface between Prolog and Python. Janus can do a lot more 🙂 As is, there is no PyPi package. The other side of the interface, embedding Python in Prolog should be ready to use in most Prolog binaries.

---

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [October 6, 2023, 11:25am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/7 "2023-10-06T11:25:15Z")

</div>

and when i do pip install januspy  
i get : cannot find a version that satisfy the requirement  
so, no solution !

---

<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:** [October 6, 2023, 11:54am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/8 "2023-10-06T11:54:12Z")

</div>

> [@pdbzro](#):
>
> pip install januspy

As I said, there is no PyPi package (yet). To install the package (called `janus_swi`), see

> [@jan](#):
>
> described in [SWI-Prolog – Janus as a Python package](https://www.swi-prolog.org/pldoc/man?section=janus-python-package).

---

<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:** [October 6, 2023, 6:15pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/9 "2023-10-06T18:15:49Z")

</div>

This worked for me:

```prolog
git clone --recurs https://github.com/SWI-Prolog/swipl-devel.git
cd swipl-devel/packages/swipy/
pip3 uninstall pyswip
pip3 uninstall janus-swi
pip3 install janus-swi
>>> from janus_swi import *
>>> for i in janus.Query("assert(mortel(bar))"): print(i)
... 
{'truth': True}
>>> for i in janus.Query("assert(mortel(qqsv))"): print(i)
... 
{'truth': True}
>>> for i in janus.Query("mortel(X)"): print(i)
... 
{'truth': True, 'X': 'bar'}
{'truth': True, 'X': 'qqsv'}
```

---

<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:** [October 7, 2023, 12:17pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/10 "2023-10-07T12:17:47Z")

</div>

> [@peter.ludemann](#):
>
> ```prolog
> git clone --recurs https://github.com/SWI-Prolog/swipl-devel.git
> cd swipl-devel/packages/swipy/
> pip3 uninstall pyswip
> pip3 uninstall janus-swi
> pip3 install janus-swi
> 
> ```

There is no need to clone the whole thing. Just cloning [GitHub - SWI-Prolog/packages-swipy: Python interface for SWI-Prolog](https://github.com/SWI-Prolog/packages-swipy) is enough to install the Python `janus_swi` package. Also running

```
pip install .

```

in the cloned repo should be enough. Janus first conflicted with `pyswip`, but that is resolved (I have both installed) and there is no need to uninstall the target package because `pip` does so before installing.

We’ll try to make this really easy. Eventually we want a PyPi and Conda package. I’m still unsure how portable that is/can be. Help on this is welcome. As is, the package installs fine from source on Linux, MacOS and Windows given a properly installed pip with a C compiler. I also have a Conda package for SWI-Prolog, which works on Linux, MacOS and almost on Windows ( ☹ ). I’m not very familiar with what Python users expect and use for adding Python packages. I have the impression that many not pure Python packages are not that easy to install ☹

---

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [October 7, 2023, 12:58pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/11 "2023-10-07T12:58:54Z")

</div>

hello

i am now experimenting with swiplserver and i have good results  
what i want to do is consult a database file - ok  
and then send some queries -ik  
and some modifications, using retract (nok) and assert (ok)

two problems remain:

1- cannot retract fact, getting “no query” error retract (homme(moi)) or retract(home(\_)) do noting (erroe no query)  
i added :- dynamic homme/1. , no change, error no query  
2- do not understand the find\_all=true or false option  
test example:

```prolog
from swiplserver import PrologMQI, PrologThread
with PrologMQI() as mqi:
with mqi.create_thread() as prolog_thread:
prolog_thread.query_async("consult('c:/web/[essai.pl](http://essai.pl)')")

while True:
result = prolog_thread.query_async_result()
if result is None:
break
else:
print("cc",result)
#ok, print tcc true
prolog_thread.query_async("retract(homme(moi))",find_all=False)
while True:
result = prolog_thread.query_async_result()
if result is None:
break
else:
print("bb",result)
#nok, di nit write bb but error no query
prolog_thread.query_async("mortel(X)",find_all=False)

while True:
result = prolog_thread.query_async_result()
if result is None:
break
else:
print("aa",result)

# ok if retract is commented

```

---

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [October 10, 2023, 11:58am UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/12 "2023-10-10T11:58:55Z")

</div>

My problem is solved using swiplserver  
My mistake concernict retract was the way i catch the results, it is necessary to do a loop while trie only for query involving variables, that can take multiple value, retract is not on this situation, so now i can work .  
Only what is the meaning of query option findall=false? Is it an other strange way to use the cut !

---

<div class="post-metadata">

**Author:** ![pdbzro](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pdbzro](https://swi-prolog.discourse.group/u/pdbzro)\
**Post date:** [November 16, 2023, 6:27pm UTC](https://swi-prolog.discourse.group/t/erreur-assertion-fail-lors-de-lappel-de-pyswip-en-python/6871/13 "2023-11-16T18:27:58Z")

</div>

hello  
i have a little swi prolog test program doing stupid things, with one consult containing few facts  
then the program do some queries, ok  
and the program end with a consult that end with a write  
curiously, the write is writed first, before the ok result to the queries  
as if all consut was analysed before rening the programm  
but the consult could be created by the program itself

from swiplserver import PrologMQI

with PrologMQI() as mqi:  
with mqi.create\_thread() as prolog\_thread:  
prolog\_thread.query\_async(“consult(‘c:/web/[essai.pl](http://essai.pl)’)”)  
while True:  
result = prolog\_thread.query\_async\_result()  
if result is None:  
break  
else:  
print(“cc”,result)  
break

prolog\_thread.query\_async(“assertz(homme(moi))”)  
print(“eee”)  
while True:  
print(“hhh”)  
result = prolog\_thread.query\_async\_result()  
print(“iii”)  
if result is None:  
break  
else:  
print(“ff”,result)  
break  
prolog\_thread.query\_async(“mortel(\_x)”,find\_all=False)

while True:  
result = prolog\_thread.query\_async\_result()  
if result is None:  
break  
else:  
print(“aa”,result)

prolog\_thread.query\_async(“retractall(homme(\_))”)  
print(“a2”)  
prolog\_thread.query\_async(“assertz(homme(xyz))”)  
print(“a3”)

prolog\_thread.query\_async(“homme(\_x)”,find\_all=False)  
while True:  
result = prolog\_thread.query\_async\_result()  
if result is None:  
break  
else:  
print(“ff”,result)  
print(“dddddd”)

# a consult ending with a write (or containing a syntax error)

prolog\_thread.query\_async(“consult(‘f:/pierre/prolog/dico/[franpy.pl](http://franpy.pl)’)”)  
result = prolog\_thread.query\_async\_result()  
print(“ww”,result)  
prolog\_thread.query\_async(“par(\_x)”,find\_all=False)  
#the wtite or syntax error is written BEFORE the ok results ti query  
while True:  
result = prolog\_thread.query\_async\_result()  
if result is None:  
break  
else:  
print(“bb”,result)  
prolog\_thread.delete()
