Start here

Your first SwiftPython app

Add the public package and call Python from Swift.

Before you start

Use macOS 15 or newer and Swift 6 / Xcode command-line tools. The public package carries Python 3.13. You do not need a host Python installation or PYTHONHOME configuration.

1. Add the package

In Xcode, add this package dependency and select Exact Version:

text
https://github.com/mikhutchinson/swiftpython-commercial.git
text
0.6.0-duplex.8.4

Or add it to your Swift package's dependencies:

swift
.package(
    url: "https://github.com/mikhutchinson/swiftpython-commercial.git",
    exact: "0.6.0-duplex.8.4"
)

Add the runtime product to the target that calls Python:

swift
.product(
    name: "SwiftPythonRuntime",
    package: "swiftpython-commercial"
)

2. Make a call

From an asynchronous function or task in your application:

swift
import SwiftPythonRuntime

let result: Double = try await Python.run {
    let math = try Python.import("math")
    return try Double(pythonObject: try math.sqrt(144.0))
}
print(result) // 12.0

The closure runs on SwiftPython's Python thread with the GIL held. It is synchronous: perform the Python operation and return a Swift value. Keep the rest of your asynchronous app work outside that closure.

3. Move heavier work to a worker

For ProcessPool use, embed the matched SwiftPythonWorker executable in your app at Contents/MacOS/SwiftPythonWorker. It comes from the same release as the framework. See packaging before distributing an app.

swift
try await withProcessPool(workers: 2) { pool in
    let result: Double = try await pool.invokeResult(
        module: "math",
        function: "sqrt",
        args: [.python(144.0)]
    )
    print(result) // 12.0
}

The scoped helper awaits pool shutdown on success and error paths. Third-party packages such as NumPy must be bundled for the app's Python runtime; adding SwiftPython does not install them automatically.

Start from a complete app

The example builders embed the runtime, worker and numerical packages:

sh
git clone https://github.com/mikhutchinson/swiftpython-commercial.git
cd swiftpython-commercial
Examples/IrisDemo/scripts/build_app.sh --open

The build downloads dependencies as needed. Afterward, the app runs offline. For distribution, follow the signing and notarization steps in the packaging guide.

Explore the examples → · Package your app →