# Prolog commands called and executed within Python using PySWIP

**URL:** <https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357>\
**Category:** Help!\
**Tags:** raspberry-pi\
**Created:** [March 6, 2019, 3:07am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357 "2019-03-06T03:07:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 6, 2019, 3:07am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/1 "2019-03-06T03:07:38Z")

</div>

Thank you all.  
All the previous exercises of Prolog with Raspberry were successfully worked out thanks to you all here. Now, this is my attempt to learn something combining Prolog commands with Python program, using PySWIP. I made something wrong since didn’t get the correct response as it shows below. Any small comment will be humbly appreciated.

pi@raspberrypi:~ $ python  
Python 2.7.13 (default, Sep 26 2018, 18:42:22)  
[GCC 6.3.0 20170516] on linux2  
Type “help”, “copyright”, “credits” or “license” for more information.

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 6, 2019, 6:18am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/2 "2019-03-06T06:18:21Z")

</div>

I think that I spelled it well, I don’t understand what the “invalid syntax” may be:

> > > from pyswip import Prolog  
> > > prolog = Prolog() \>\>\> prolog.assertz(“father(michael,john)”) \>\>\> prolog.assertz(“father(michael,gina)”) \>\>\> List(prolog.query(“father(michael,X)”)) [{‘x’: ‘john’}, {‘X’: ‘gina’}]  
> > > for soln in prolog.query(“father(X,Y)”):  
> > > … print soln[“X”], “is the father of”, soln[“Y”] …  
> > > ^  
> > > SyntaxError: invalid syntax

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [March 6, 2019, 7:23pm UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/3 "2019-03-06T19:23:15Z")

</div>

You are showing Python code. I think Python is complaining about your code.

This is a forum about Prolog code.

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 9, 2019, 7:51pm UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/4 "2019-03-09T19:51:37Z")

</div>

Abaljeu,  
Thanks for your assistance but this demonstration works with a bridge program between Prolog and Python called PySWIP, which allows Prolog commands to be called and executed within a python program in order to practice Artificial Intelligence with Raspberry Pi and some minimal electronics.  
I am only a beginner and this is an experiment contained in the book “Beginning artificial Intelligence with the Raspberry Pi” whose author is professor Donald J. Norris from Southern North Hampshire University.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 10, 2019, 4:43am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/5 "2019-03-10T04:43:53Z")

</div>

> [@yosoyquiensoy](#):
>
> the book “Beginning artificial Intelligence with the Raspberry Pi” whose author is professor Donald J. Norris from Southern North Hampshire University.

Have you considered directing your questions to professor Donald J. Norris directly? After all, he wrote the book, he should know best how he meant it.

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [March 11, 2019, 5:33pm UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/6 "2019-03-11T17:33:40Z")

</div>

I stand by what I wrote. Yes PySwip invokes Prolog, but Python usage is the problem here. I don’t know python enough to answer, which is why I suggest asking a Python group.

---

<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:** [March 12, 2019, 5:29am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/7 "2019-03-12T05:29:15Z")

</div>

> [@yosoyquiensoy](#):
>
> for soln in prolog.query(“father(X,Y)”):  
> … print soln[“X”], “is the father of”, soln[“Y”] …

The “`...`” is typically from the Python interpreter if you’re entering this [interactively](https://docs.python.org/3/tutorial/interpreter.html#interactive-mode). Note that indentation is _required_ but the amount of indentation is up to you.

The "`>>>`"s are also from the Python interpreter.

What you should do is either type the lines interactively or put everything into a file.  
Here, I’ve put it into `example.py` (my shell prompt character is “`$`”):

```prolog
$ cat example.py
from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
for soln in prolog.query("father(X,Y)"):
    print(soln["X"], "is the father of", soln["Y"])

$ python3.7 example.py
michael is the father of john
michael is the father of gina

```

(I’ve also changed your program to be valid Python 3 (because Python 2 will soon be deprecated.)  
Also, if you’re dong a copy and paste, watch out for editors that do “smart quotes” (changing the straight quotes `"..."` into `“...”`).

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 14, 2019, 3:08am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/8 "2019-03-14T03:08:51Z")

</div>

Peter, I am very grateful for your suggestion. I’ve tried this over and over with no success. Also notice that I am working this with Python3 and with two different queries according to two models that i received in this forum,  
This is your model: ( maybe it is too short or I didn’t get your Idea. I am so neophyte in this)  
Any idea from you will be greatly and humbly appreciated.

pi@raspberrypi:~ python3 -m venv pyswip\_env pi@raspberrypi:~ source pyswip\_env/bin/activate  
(pyswip\_env) pi@raspberrypi:~ $ python  
Python 3.5.3 (default, Sep 27 2018, 17:25:39)  
[GCC 6.3.0 20170516] on linux  
Type “help”, “copyright”, “credits” or “license” for more information.

> > > from pyswip import Prolog \>\>\>prolog = Prolog()  
> > > prolog.assertz(“father(michael,john)”) \>\>\>prolog.assertz(“father(michael,gina)”) for soln in prolog.query(“fatherX,Y)”): …print(soln, “is the father of”, soln[“Y”]) …  
> > > File “”, line 1  
> > > from pyswip import Prolog \>\>\>prolog = Prolog()  
> > > prolog.assertz(“father(michael,john)”) \>\>\>prolog.assertz(“father(michael,gina)”) for soln in prolog.query(“fatherX,Y)”): …print(soln, “is the father of”, soln[“Y”]) …  
> > > ^  
> > > SyntaxError: invalid syntax

Also this other one more elaborated, I think.

(pyswip\_env) pi@raspberrypi:~ $ python  
Python 3.5.3 (default, Sep 27 2018, 17:25:39)  
[GCC 6.3.0 20170516] on linux  
Type “help”, “copyright”, “credits” or “license” for more information.

> > > from pyswip import Prolog \>\>\> prolog =Prolog()  
> > > prolog.assertz(“father(michael,john)”)  
> > > prolog.assertz(“father(michael,gina)”)  
> > > list(prolog.query(“father(michael,X)”)) [{‘X’: ‘john’}, {‘X’: ‘gina’}]  
> > > for soln in prolog.query(“fatherX,Y)”): … print(soln[“X”],“is the father of”, soln[“Y”]) …  
> > > File “”, line 1  
> > > from pyswip import Prolog \>\>\> prolog = Prolog() \>\>\> prolog.assertz(“father(michael,john)”) \>\>\> prolog.assertz(“father(michael,gina)”) \>\>\> list(prolog.query(“father(michael,X)”))  
> > > [{‘X’: ‘john’}, {‘X’: ‘gina’}]  
> > > for soln in prolog.query(“fatherX,Y)”): … print(soln[“X”], “is the father of”, soln[“Y”]) …  
> > > ^  
> > > SyntaxError: invalid syntax

---

<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:** [March 14, 2019, 3:38am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/9 "2019-03-14T03:38:30Z")

</div>

Try this.

Using whatever editor you prefer, create file `example.py` in your current directory and put this text into it:

```
from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
for soln in prolog.query("father(X,Y)"):
    print(soln["X"], "is the father of", soln["Y"])

```

Then run this command:

```
python example.py

```

You should get this output:

```
michael is the father of john
michael is the father of gina

```

If you get an error message like `ModuleNotFoundError: No module named 'pyswip'`, then you’ll need to install that package, e.g., by a command like this:

```
python -m pip install pyswip
```

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 16, 2019, 5:15am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/10 "2019-03-16T05:15:03Z")

</div>

Hello Peter,

Thank you. This really was so simple. I’m grateful to all the people who have helped me with this obstacle, and thanks to you for helping to finish weeks of struggle.

This is what I’ve got now with Geany ( I couldn’t make it with any other editor):

from pyswip import Prolog

prolog = Prolog()

prolog.assertz(“father(michael,john)”)

prolog.assertz(“father(michael,gina)”)

for soln in prolog.query(“father(X,Y)”):

print(soln[“X”], “is the father of”, soln[“Y”])

Then I clicked on the paper plane icon and voila:

(‘michael’, ‘is the father of’, ‘john’)

(‘michael’, ‘is the father of’, ‘gina’)

I finally got the previous output. The only problem now is how to get rid of the parenthesis, commas and quotes. Your help continues being highly appreciated.

Victor

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 17, 2019, 4:56am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/11 "2019-03-17T04:56:48Z")

</div>

Hello Peter,  
I pasted your query directly to the Raspberry terminal with Python 2.7.13 and worked!  
I just typed just python (no iteration number).  
My problem was that I insisted to use Python 3.  
so this time I didn’t need editor or new folder. Just pasted your query and voila!  
so it worked both ways.  
Thank you.

pi@raspberrypi:~ $ python  
Python 2.7.13 (default, Sep 26 2018, 18:42:22)  
[GCC 6.3.0 20170516] on linux2  
Type “help”, “copyright”, “credits” or “license” for more information.

> > > from pyswip import Prolog  
> > > prolog = Prolog()  
> > > prolog.assertz(“father(michael,john)”)  
> > > prolog.assertz(“father(michael,gina)”)  
> > > for soln in prolog.query(“father(X,Y)”):  
> > > … print(soln[“X”], “is the father of”, soln[“Y”])  
> > > …  
> > > (‘michael’, ‘is the father of’, ‘john’)  
> > > (‘michael’, ‘is the father of’, ‘gina’)

---

<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:** [March 17, 2019, 6:03pm UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/12 "2019-03-17T18:03:32Z")

</div>

> [@yosoyquiensoy](#):
>
> My problem was that I insisted to use Python 3.

I don’t think so. Python3 is very similar to Python2. In this particular case, the only difference is that Python3 treats `print` as a function call but Python2 treats it as a reserved word … so, in Python3, the parentheses are required in `print(soln[“X”], “is the father of”, soln[“Y”])` but Python2 allows `print soln[“X”], “is the father of”, soln[“Y”]`. However, Python2 _allows_ the parentheses, so `print(...)` is allowed for both Python2 and Python3. In many cases, you can write code that works for both Python2 and Python3; and there’s a [compatibility library](https://six.readthedocs.io/).

If you’re going do any significant work with Prolog, I suggest that you use it directly (e.g., with SWI-Prolog); adding in the Python layer makes things more complicated and you don’t get some of the more advanced and useful features of Prolog. And if you’re going to do significant work with Python, I suggest you read some tutorials, such as [The Python Tutorial — Python 3.12.0 documentation](https://docs.python.org/3/tutorial/) (and there are many others on the Internet).

---

<div class="post-metadata">

**Author:** ![yosoyquiensoy](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/yosoyquiensoy/32/124_2.png) [@yosoyquiensoy](https://swi-prolog.discourse.group/u/yosoyquiensoy)\
**Post date:** [March 20, 2019, 4:44am UTC](https://swi-prolog.discourse.group/t/prolog-commands-called-and-executed-within-python-using-pyswip/357/13 "2019-03-20T04:44:38Z")

</div>

pi@raspberrypi:~ python example.py ('michael', 'is the father of', 'john') ('michael', 'is the father of', 'gina') pi@raspberrypi:~

I’m reading the tutorials. Thank you , again.
