Install & Compatibility
Where this runs
tested against v4.11.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
System_
✓ import System_
✗ from MyProgram_py import MyModule
This quickstart demonstrates the typical workflow for using `dafnyruntimepython`: writing a Dafny program, compiling it to Python using the `dafny` CLI, and then executing the generated Python code. The `dafnyruntimepython` library is implicitly used by the generated Python files. Ensure the `dafny` CLI is installed and in your system's PATH to run this example.
import os
import subprocess
# 1. Create a dummy Dafny file
dafny_code = """
// MyDafnyProgram.dfy
module {:extern "MyDafnyModule"} MyModule {
class C {
static method SayHello() {
print "Hello from Dafny!\n";
}
}
}
method Main() {
MyModule.C.SayHello();
}
"""
with open("MyDafnyProgram.dfy", "w") as f:
f.write(dafny_code)
print("Dafny source file created: MyDafnyProgram.dfy")
# 2. Compile Dafny to Python
# Requires the 'dafny' CLI tool to be installed and in PATH.
# This step generates a directory like MyDafnyProgram-py/
compile_cmd = ["dafny", "build", "--target:py", "MyDafnyProgram.dfy"]
print(f"Executing: {' '.join(compile_cmd)}")
try:
subprocess.run(compile_cmd, check=True, capture_output=True, text=True)
print("Dafny compiled successfully to Python.")
except subprocess.CalledProcessError as e:
print(f"Dafny compilation failed:\n{e.stdout}\n{e.stderr}")
exit(1)
# 3. Execute the generated Python code
# Add the generated directory to PYTHONPATH to allow imports
os.environ["PYTHONPATH"] = os.getcwd() + ":" + os.path.join(os.getcwd(), "MyDafnyProgram-py")
# The entry point for the generated Python code is typically in the top-level generated .py file.
# The name will be derived from the .dfy file (e.g., MyDafnyProgram.py in MyDafnyProgram-py/)
# For Dafny 4.x, the Main method is typically directly runnable or imported.
# Let's find the main generated file.
output_dir = "MyDafnyProgram-py"
if os.path.exists(output_dir):
python_main_file = os.path.join(output_dir, "MyDafnyProgram.py")
if os.path.exists(python_main_file):
print(f"Executing generated Python: python3 {python_main_file}")
try:
run_cmd = ["python3", python_main_file]
result = subprocess.run(run_cmd, check=True, capture_output=True, text=True)
print("\n--- Generated Python Output ---")
print(result.stdout)
if result.stderr:
print("--- Generated Python Errors ---")
print(result.stderr)
print("-------------------------------")
except subprocess.CalledProcessError as e:
print(f"Execution of generated Python failed:\n{e.stdout}\n{e.stderr}")
exit(1)
else:
print(f"Error: Main Python file not found in {output_dir}")
exit(1)
else:
print(f"Error: Dafny output directory {output_dir} not found.")
exit(1)
# Clean up generated files (optional)
# import shutil
# if os.path.exists("MyDafnyProgram.dfy"):
# os.remove("MyDafnyProgram.dfy")
# if os.path.exists(output_dir):
# shutil.rmtree(output_dir)
Debug
Known issues
gotchaThe `dafnyruntimepython` library is not intended for direct interaction or import by user-written Python code. It is an internal dependency for Python code generated by the Dafny compiler. Attempts to `import dafnyruntimepython` directly will not yield useful functionality as its components are typically exposed through the generated Dafny modules.fixAlways interact with Dafny-generated functionality through the modules produced by the Dafny compiler, rather than trying to import `dafnyruntimepython` directly. The generated Python code will handle its internal dependencies.
affects: All versions
gotchaWhen compiling Dafny to Python, ensure the `dafny` CLI tool is installed and its version is compatible with the `dafnyruntimepython` library version. Mismatched versions can lead to unexpected runtime behavior or compilation errors, though the runtime library itself aims for backward compatibility.fixKeep your `dafny` CLI tool and `dafnyruntimepython` library updated to their latest compatible versions. The `dafny` CLI is typically installed via `dotnet tool install --global dafny` or by downloading a binary distribution.
affects: All versions
gotchaGenerated Python classes from Dafny currently cannot be directly compared or mixed with native Python types in a semantically equivalent way. This can lead to unexpected behavior when passing Dafny-generated objects to native Python functions or vice-versa, especially for complex data structures.fixWhen integrating Dafny-generated code with native Python, be mindful of type boundaries. It may be necessary to explicitly convert or map between Dafny-specific types and standard Python types at the interface points to ensure correct behavior and avoid runtime errors.
affects: All versions (as of Dafny 4.x)
Upgrade
Version history
4.11.0latest on PyPI · released Aug 25, 2025
Audit
Dependencies
No dependency data recorded yet.