我正在尝试执行我使用 Prolog 的 Python 脚本。代码如下:
from pyswip import Prolog
prolog = Prolog()
prolog.consult("KB.pl")
print(list(prolog.query("faster(cat, dog)")))
我一直遇到以下错误:SWI-Prolog:[致命错误:找不到系统资源]。
我已经尝试按照 SWI Prolog 文档提供的说明修复该问题。
我还尝试使用以下命令行从 Windows cmd 设置环境变量 SWI_HOME_DIR:
setx SWI_HOME_DIR "C:\Program Files\swipl"
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][1] 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][2]), 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 proceed with the simple pip command `pip install janus-swi`.
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)")))
您可以参考文档中的第7节了解其他功能。