# Python prolog library

**URL:** <https://swi-prolog.discourse.group/t/python-prolog-library/3258>\
**Category:** Tools\
**Created:** [November 13, 2020, 4:29pm UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258 "2020-11-13T16:29:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![westly](https://avatars.discourse-cdn.com/v4/letter/w/977dab/32.png) [@westly](https://swi-prolog.discourse.group/u/westly)\
**Post date:** [November 13, 2020, 4:29pm UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258/1 "2020-11-13T16:29:56Z")

</div>

Is there any python library which is support prolog unification?

Thanks

---

<div class="post-metadata">

**Author:** ![westly](https://avatars.discourse-cdn.com/v4/letter/w/977dab/32.png) [@westly](https://swi-prolog.discourse.group/u/westly)\
**Post date:** [November 14, 2020, 10:36am UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258/2 "2020-11-14T10:36:12Z")

</div>

Anyone can help?

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [November 14, 2020, 11:07am UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258/3 "2020-11-14T11:07:59Z")

</div>

My guess is: There isn’t. You’ll probably need to go the way over C:

> **[SWI-Prolog -- Manual](https://www.swi-prolog.org/pldoc/man?section=embedded)**

Hope this helps,  
Volker

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 14, 2020, 12:23pm UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258/4 "2020-11-14T12:23:36Z")

</div>

> [@westly](#):
>
> Is there any python library which is support prolog unification?

The reason I did not answer yesterday when I first saw the question is because I don’t know of any that I would trust.

For example this code: [bi](https://github.com/vsraptor/bi)/[lib](https://github.com/vsraptor/bi/tree/master/lib)/[bi\_engine.py](https://github.com/vsraptor/bi/blob/03b8ec4789592381c370a3c98114e4ba6f3d3fb6/lib/bi_engine.py)

Note: I found the reference from [Implementing the Prolog Unification algorithm in Python? Backtracking](https://stackoverflow.com/questions/49101342/implementing-the-prolog-unification-algorithm-in-python-backtracking) which I answered many years ago.

bi\_engine.py looks to have all of the correct pieces but I have never used it. From many years of seeking such code in the distant past of more than a decade, I have learned that one should have a very extensive set of test cases that the code should pass. Almost enviably the code will fail a test and then trying to fix the code is not worth effort. Many will not do cut correctly.

In the long run I have found that the better way to use a Prolog query from another programming language such as Python is to use a foreign language interface, e.g. for Python to SWI-Prolog see [pyswip](https://pypi.org/project/pyswip/). Now having used such interfaces I have come to learn that it is much easier to just solve the problem in Prolog first than trying to work in the other language and force everything through the interface. In other words, a single call across the interface is much easier to debug than hundreds of calls to different predicates across the interface.

If you want to move into more of a modern way of doing this, then I would suggest starting up a Docker container that runs SWI-Prolog and implement a JSON interface. I have not done this but I think the [SWISH](https://github.com/SWI-Prolog/docker-swish) image might.

HTH

---

<div class="post-metadata">

**Author:** ![cjm](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/cjm/32/440_2.png) [@cjm](https://swi-prolog.discourse.group/u/cjm)\
**Post date:** [November 17, 2020, 7:59am UTC](https://swi-prolog.discourse.group/t/python-prolog-library/3258/5 "2020-11-17T07:59:11Z")

</div>

You can also use [Python pengines](https://pypi.org/project/pengines/)

It’s pretty straightforward to launch a prolog server (either with SWI installed, or via Docker) and have Python code communicate with it.
