SWI-Prolog Python interface
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog Python interface
        • Introduction
        • Data conversion
        • Janus by example - Prolog calling Python
        • library(janus): Call Python from Prolog
        • Calling Prolog from Python
        • Janus and threads
        • Janus and signals
        • Janus versions
        • Janus as a Python package
        • Prolog and Python
        • Janus performance evaluation
        • Python or C/C++ for accessing resources?
        • Janus platforms notes
        • Compatibility to the XSB Janus implementation
        • Status of Janus

9 Janus as a Python package

The Janus GIT repo provides setup.py. Janus may be installed as a Python package after downloading using

pip install .

pip allows for installation from the git repository in a one-liner as below.

pip install git+https://github.com/SWI-Prolog/packages-swipy.git#egg=janus_swi

Installing janus as a Python package requires

  • The swipl program in the default search path. The setup.py runs swipl --dump-runtime-variables to obtain the installation locations of the various Prolog components. On Windows, if swipl is not on %PATH%, setup.py tries the registry to find the default binary installation.
  • A C compiler that can be used by pip. The janus interface has been tested to compile using GCC, Clang and Microsoft Visual C++.

After successful installation we should be able to use Prolog directly from Python. For example:

python
>>> from janus_swi import *
>>> query_once("writeln('Hello world!')")
Hello world!
{'truth': True}
>>> [a["D"] for a in query("between(1,6,D)")]
[1, 2, 3, 4, 5, 6]
>>> prolog()
?- version.
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.12-8-g70b70a968-DIRTY)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
...
?-