Pip install swipl?

Similar to @mgondan1 has made SWI-Prolog available as R package, it would be great to have SWI-Prolog installable as Python package.

Installing SWI-Prolog however is a rather complicated process. It involves a serious amount of dependencies, a lot of cmake based configuration to deal with OS, compiler and dependencies and a complicated interleave of building C objects and running Prolog. For short, we need CMake when building from source. But, @Theresa suggested we might also be able to get away by having pip use installers. Notably for Windows that might be an interesting option, in particular because a Microsoft Visual C++ is not well supported for building SWI-Prolog.

Does anyone have ideas how this can be achieved?

1 Like

Not really an answer to your question, but maybe worth considering before you start: I ended up with two R packages, rswipl that “embeds” SWI-Prolog, and rolog that “connects” to an existing installation of SWI-Prolog (e.g., the swipl found in the path, in the Windows registry, or the embedded one from rswipl). rswipl is the big one that basically does the entire installation (including cmake etc.). rolog is rather lightweight, searching for the libswipl.so, and setting SWI_HOME_DIR to something sensible.

If you’re in need of funny-sounding name for your packages, consider swipy and pyrolog


Copying @JamesH from another mail, just to keep things together

Regarding building a Python package to install with pip, did you look into the file pyproject.toml much?

It sounds like this is the starting point for building a package. It’s format depends on the chosen “build tool”.
The Packaging Flow - Python Packaging User Guide

You can choose setuptools as an option for the build tool. It isn’t deprecated in itself, but setup.py is deprecated as the starting point in favor of pyproject.toml.

There are many other options for the build tool, eg:
Introduction to Python packaging with meson-python - meson-python 0.16.0.dev0

which seems to have good support for C integration and is used as the build tool for scipy.

More stuff found

I hacked something together that works fine on a Linux machine with all dependencies already installed through apt. See GitHub - SWI-Prolog/py-swi-prolog: Build SWI-Prolog as a Python package

See README.md there for status and what needs to be done. I have no clue whether this is a reasonable approach. It gets notably interesting if we can get the dependencies either from the OS package manager or from other pip packages, so you can install this from source with only Python and pip (with C compiler) installed. Is that feasible? How do other Python packages deal with this?

For example, if we want ODBC support we need the ODBC library from somewhere. On Linux systems this is typically a package, so you need the right apt/dnf/… package name (and which package manager is used). On MacOS you’ll need to get UnixODBC from somewhere (Macports, Homebrew, pip?). On Windows ODBC luckily is a standard library but for many other packages you are in the same boat as for MacOS.

Ideally you should be able to fall back to pip packages for all dependencies …

Any clue?

1 Like

I don’t have a definitive answer to this, but since no one has responded maybe these links would help:


mypy is a package that’s becoming more popular, it does type checking using python type hints. I use it sometimes. The command line utility is installed when you install with pip. It’s at least an example of a widely used package that installs a command line binary.


Thanks. Installing the commandline script is not a bit problem. I followed the way cmake does that: it creates a module cmake and uses the pyproject.toml to describe the entry points. That installs little python scripts that runs the Prolog
executable from Python. Works fine. Comes at a reasonable price. Debugging
Prolog using gdb is a little harder as there is this script in between and startup is a little slower (40ms vs 24ms when starting Prolog natively). The nice thing is that you can easily call SWI-Prolog as a process as well without worrying about its location.

The problem is the library dependencies of SWI-Prolog, how do I get access the X11, OpenSSL, ODBC, Yaml, and all the other dependencies? The CMake extension for setuptools suggests it can install packages and make the cmake configuration easily accessible to other pip packages. That would suggest we could have cross-platform pip packages for these libraries (providing no Python modules). I don’t see much evidence this is used though. But, possibly I looked at the wrong place :slight_smile:

Maybe ask here:

1 Like