Skip to content

🍎 ApplePy

Create native Python extensions in Swift.

ApplePy lets you build pip install-able Python packages written entirely in Swift — with direct access to Apple frameworks like CoreML, Metal, NaturalLanguage, Security, and more.

  • Get started in 30 seconds


    pip install applepy-cli
    applepy new myproject
    cd myproject && applepy develop
    

    Quickstart

  • Write Swift, import from Python


    @PyFunction
    func hello(name: String) -> String {
        "Hello, \(name)! 🍎"
    }
    

    Macros Reference

  • Access Apple frameworks


    CoreML, Metal, NaturalLanguage, Security — any framework available in Swift is now available from Python.

    Examples

  • pip install & publish


    Build wheels, publish to PyPI. Your users just pip install — no Swift toolchain needed at runtime.

    Building & Packaging

Quick Example

import ApplePy
@preconcurrency import ApplePyFFI

@PyFunction
func greet(name: String, greeting: String = "Hello") -> String {
    "\(greeting), \(name)! 🍎"
}

@PyModule("mylib", functions: [greet])
func mylib() {}
>>> import mylib
>>> mylib.greet("World")
'Hello, World! 🍎'
>>> mylib.greet("World", "Hi")
'Hi, World! 🍎'

Features

Macro Purpose
@PyFunction Expose top-level functions (with default argument support)
@PyClass Expose structs/classes as Python types
@PyMethod Mark instance methods for Python
@PyStaticMethod Mark static methods for Python
@PyProperty Expose stored properties with getters/setters
@PyEnum Swift enum → Python IntEnum or class hierarchy
@PyModule Generate PyInit_ module entry point

Type Conversions

Swift Python Direction
Int, Double, Float, Bool int, float, bool
String str
[T] list
[K: V] dict
Set<T> set
Optional<T> None / value
PyTuple2<A,B>, PyTuple3<A,B,C> tuple

vs PyO3

Dimension PyO3 (Rust) ApplePy (Swift)
Macros #[pyfunction], #[pyclass] @PyFunction, @PyClass
Memory safety 'py lifetimes GILGuard: ~Copyable
Build tool maturin applepy CLI
Platform Cross-platform macOS (Apple frameworks)
Enums Class-based IntEnum + class hierarchy

Full comparison