pythonprologswi-prolog

SWI-Prolog: [FATAL ERROR: Could not find system resources]


I'm trying to execute my Python script in which I use Prolog. The code is the following:

from pyswip import Prolog
prolog = Prolog()
prolog.consult("KB.pl")
print(list(prolog.query("faster(cat, dog)")))

I kept having the following error: SWI-Prolog: [FATAL ERROR: Could not find system resources].

I already tried to fix the issue following the instructions provided by SWI Prolog documentation.

I also tried to set the environment variable SWI_HOME_DIR from the Windows cmd using the following command line:

setx SWI_HOME_DIR "C:\Program Files\swipl"

Solution

  • One solution to the problem is to replace pyswip with janus_swi. janus is a bi-directional interface that allows for calling prolog from python and calling python from prolog. For more information, check out the documentation at the official website.

    Before installing janus-swi via pip install janus-swi, make sure you have Microsoft C++ Build Tools (I don't know if there are alternatives, but this is what worked for me) installed on your machine. You can get them when installing Visual Studio (follow this link), make sure you select C/C++ build tools (approximately 5.5GB) during the installation process.

    Note that you don't need to add any environment variable at this point as the build tools will be recognized automatically. After C++ build tools have been installed successfully, you can then proceed with the simple command:

    pip install janus-swi
    

    Finally, it is still important to have the environment variable SWI_HOME_DIR defined. Use the following command to solve the actual error:

    setx SWI_HOME_DIR "C:\Program Files\swipl"
    

    The use of janus_swi is slightly different from pyswip, yet it is still simple. Your code after refactoring will be:

    import janus_swi as janus
    
    janus.consult("KB.pl")
    print(list(janus.query("faster(cat, dog)")))
    

    KB.pl is expected to be in the same directory as your python source file. You can refer to section 5 in the documentation to learn about the other functions.