Embedding¶
PytoRuntime can be embedded in your app in mainly two ways, which can be combined: via the Swift Package Manager and via Xcode Frameworks.
You can browse all available packages in the package registry.
Swift Package Manager¶
To include PytoRuntime in your Swift Package, you first need to set the package registry because it is from there that binaries are fetched. This can be done in multiple ways.
Locally, from your package’s directory:
$ swift package-registry set "https://git.gatit.es/api/packages/pyto/swift"
Globally in your system:
$ swift package-registry set --global "https://git.gatit.es/api/packages/pyto/swift"
And for Xcode projects, you’ll need to create or modify the file xcshareddata/swiftpm/configuration/registries.json inside your project’s .xcworkspace or .xcodeproj directory and declare the package registry there:
{
"registries" : {
"[default]" : {
"supportsAvailability" : false,
"url" : "https://git.gatit.es/api/packages/pyto/swift"
}
}
}
Then you can add this to your package manifest (replace with the version you want to use):
let package = Package(
name: "my-package",
dependencies: [
.package(url: "https://git.gatit.es/pyto/pyto-runtime.git", from: "20.0.0"),
],
targets: [
.executableTarget(
name: "my-target",
dependencies: [
.product(name: "PytoRuntime", package: "pyto-runtime")
]),
]
)
Besides the frontend, this will download the necessary pre-built binaries required for PytoRuntime to function from the package registry. But this alone does not embed any Python version.
You can include it from the package registry:
let package = Package(
name: "my-package",
dependencies: [
// ...,
.package(id: "pyto.apple-python-314", from:"3.14.0"),
],
targets: [
.executableTarget(
name: "my-target",
dependencies: [
// ...,
.product(name: "python-314", package: "pyto.apple-python-314")
]),
]
)
Warning
Including Python like that directly in your executable will automatically link every one of its binaries and this can slow down startup especially if multiple Python versions or more third party extensions are included. As a workaround you can create a separate dynamic library product that includes the Python binaries and not link it to your main executable but only embed it. If you are using Xcode, embedding this dynamic library will embed the Python frameworks with it.
You can browse the package registry linked above for all available packages. Search for the Swift ones: they all say how to include them in SPM or you can download them to include locally. As an example, here is the link to the apple-python-314 Swift Package.
Xcode Frameworks¶
PytoRuntime does not currently have an Xcode framework build, so for the moment you have to include it as a Swift Package for the frontend. But other packages can be downloaded from the package registry as Xcode frameworks.
Search for the “Generic” packages. They have a link for every framework they need to function and a list of curl commands that you can directly paste into your terminal. For example, here is the link for the python-314 generic package.
Frameworks downloaded from the registry should be embedded without linking, as PytoRuntime dynamically loads them when needed.
Including Python Packages¶
As you can see in the package registry, there are also wheels for Python packages with native extensions. These cannot be installed with pip directly in a sandboxed environment like an app. Instead, you use pyto-pkg to install them into a Swift Package that you can include locally.
This tool creates a Swift Package, but you can also directly include the Xcode Frameworks into your project and the .bundle directory inside Sources/<package name> as a resource. The bundle contains the Python scripts and is named so PytoRuntime automatically recognizes it and adds it to sys.path.
There is already a distribution of all Python packages in the registry. For Python 3.14, there is apple-python-314-packages for SPM and python-314-packages for Xcode Frameworks.