Skip to main content

2PPy: tuProlog for Python

PyPI Python CI/CD Docs License

2PPy brings tuProlog (2P-Kt) to Python: logic terms, unification, theories, Prolog and ProbLog solvers, predicates written in Python, and Prolog code driving Python objects, through a Pythonic API over 2P-Kt (via JPype).

Documentation: https://tuprolog.github.io/2ppy/

What to expect

  • It is a wrapper, not a re-implementation. 2PPy wraps 2P-Kt's JVM implementation via JPype: importing tuprolog starts a Java Virtual Machine in the Python process. A Java runtime is installed automatically as a Python package (jdk4py), so no Java installation is needed, but 2PPy fails at runtime if that JVM cannot start or work on your platform (e.g. unsupported architecture, restricted environments). The JVM can be started only once per Python process.
  • Bugs may be inherited from 2P-Kt. Solving, parsing, and libraries are 2P-Kt's: if something behaves unexpectedly, the cause may lie upstream. Please report it here anyway; we will forward it to 2P-Kt when needed.
  • The API is meant to be Pythonic. Snake-case names, keyword arguments with defaults, Python protocols (iteration, indexing, len, ...) on logic objects, automatic conversions between Python values and terms: the goal is to exploit logic programming programmatically, from Python code, not only to run Prolog programs.
  • It is not fast. 2P-Kt favours flexibility and portability over speed (there are plans to address that), and 2PPy is most likely even slower, due to the overhead of every Python <-> JVM call. Use it for prototyping, teaching, research, and moderate workloads, not where performance is critical.
  • The documentation is mostly generated, at the moment, but kept up to date with the codebase: every documented example is run by the tests.

Installation

2PPy is published on PyPI as 2ppy (the package to import is tuprolog). It requires Python 3.10 or later, on Linux, macOS, or Windows.

With pip, preferably in a virtual environment:

python -m venv .venv && source .venv/bin/activate   # on Windows: .venv\Scripts\activate
pip install 2ppy                                    # upgrade with: pip install -U 2ppy

With uv, in a project, or as a standalone tool for the terminal IDE:

uv add 2ppy
uv tool install 2ppy        # puts 2ppy-tui on the PATH

The package bundles 2P-Kt's JARs, and a Java runtime comes with its jdk4py dependency: no Java installation is needed. To check that everything works (the first import starts the JVM):

python -c "import tuprolog; print(tuprolog.__version__, tuprolog.Info.VERSION, tuprolog.JVM_VERSION)"

This prints the versions of 2PPy, 2P-Kt, and the JVM.

Quick start

>>> from tuprolog.solve.prolog import prolog_solver
>>> from tuprolog.theory.parsing import parse_theory
>>> solver = prolog_solver(
...     static_kb=parse_theory("""
...     parent(abe, homer). parent(homer, bart). parent(homer, lisa).
...     ancestor(X, Y) :- parent(X, Y).
...     ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
... """)
... )
>>> list(solver.query("ancestor(abe, D)"))
[{'D': 'homer'}, {'D': 'bart'}, {'D': 'lisa'}]

Predicates can be written in Python:

>>> from tuprolog.solve.library import library
>>> from tuprolog.solve.primitive import predicate
>>> lib = library("my.lib", primitives={"upper/2": predicate(lambda s, _: [(s, s.upper())])})
>>> list(prolog_solver(libraries=lib).query("upper(hello, U)"))
[{'U': 'HELLO'}]

Terminal IDE

2PPy comes with a terminal IDE (TUI), emulating 2P-Kt's graphical IDEs (ide-swing and ide-web) in the terminal, on the same GUI core: edit theories, run queries, and browse their solutions.

2ppy-tui                                    # or: python -m tuprolog.tui
docker run -it --rm ghcr.io/tuprolog/2ppy   # the same, with Docker

See Use the terminal IDE. A plain REPL is also available: python -m tuprolog.repl.prolog (or ...problog).

Learn more

See the tutorials and the example notebooks to get started, and the API reference for the details.

Contributing

uv sync && uv run poe build-jars    # environment and JARs (requires a JDK 17+)
uv run poe static-checks            # what CI checks
uv run poe test                     # tests, including every documented example
uv run poe docs                     # the documentation site

See Contribute and AGENTS.md. Releases are automated by semantic-release from Conventional Commits.

Metadata

Release files for 2ppy 1.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for 2ppy 1.7.1
File Size Uploaded Provenance
2ppy-1.7.1.tar.gz 15.3 MB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for 2ppy 1.7.1
File Interpreter ABI Platform
2ppy-1.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 30.6 MB

Release files / 2ppy-1.7.1.tar.gz

Download URL 2ppy-1.7.1.tar.gz
Size 15.3 MB
Tags Source
SHA-256 checksum
How to use checksums
c3ef6387105194af07afbb19d03eb4b7dfc10afb667f2087bbae8f2e30f84e97
BLAKE2b-256 checksum
How to use checksums
7df80c16ffbc28f3cf743826049f3e965a63d3f1b2da673a48f1694957a88efc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / 2ppy-1.7.1-py3-none-any.whl

Download URL 2ppy-1.7.1-py3-none-any.whl
Size 15.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
1fc7cf87852e1644c99e2046b631e1698c11318cc6a5d4eed4a332ea5f6e270d
BLAKE2b-256 checksum
How to use checksums
6ddbb6197fd47d1171787fd89b8bcabd02c8e222f671e6c95aca972c098ec9b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for 2ppy 1.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.7.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page