Swiftpack.co -  pvieito/PythonKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Swift framework to interact with Python.
.package(url: "https://github.com/pvieito/PythonKit.git", from: "v0.1.0")


Swift framework to interact with Python.


PythonKit requires Swift 5 or higher and has been tested on macOS, Linux and Windows.


Some Python code like this:

import sys

print(f"Python {sys.version_info.major}.{sys.version_info.minor}")
print(f"Python Version: {sys.version}")
print(f"Python Encoding: {sys.getdefaultencoding().upper()}")

Can be implemented in Swift through PythonKit with the following code:

import PythonKit

let sys = try Python.import("sys")

print("Python \(sys.version_info.major).\(sys.version_info.minor)")
print("Python Version: \(sys.version)")
print("Python Encoding: \(sys.getdefaultencoding().upper())")

Swift Package Manager

Add the following dependency to your Package.swift manifest:

.package(url: "https://github.com/pvieito/PythonKit.git", .branch("master")),

Environment Variables

As the Python library are loaded at runtime by PythonKit, it will try to find the most modern Python version available in the system. You can force a given version with the PYTHON_VERSION environment variable or an specific Python library path or name with PYTHON_LIBRARY.

$ PYTHON_VERSION=3 swift run
[*] Python 3.5
$ PYTHON_VERSION=2.7 swift run
[*] Python 2.7
$ PYTHON_LIBRARY=libpython3.5.so swift run
[*] Python 3.5
$ PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so swift run
[*] Python 2.7

If PythonKit cannot find and load the Python library you can set the PYTHON_LOADER_LOGGING environment variable to know from which locations PythonKit is trying to load the library:

Loading symbol 'Py_Initialize' from the Python library...
Trying to load library at 'Python.framework/Versions/3.8/Python'...
Trying to load library at '/usr/local/Frameworks/Python.framework/Versions/3.8/Python'...
Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.


  • If your are targeting the Mac platform with the Hardened Runtime enabled make sure you are properly signing and embedding the Python framework you are trying to load with PythonKit. The Hardened Runtime Library Validation mechanism prevents a process from loading libraries that are not signed by Apple or the same developer as the main process.


  • Originally PythonKit was based on the Python module from the Swift for TensorFlow experimental project.
  • If you have questions about PythonKit you can ask on the Swift Forums.


Stars: 748
Last commit: 5 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

PythonKit v0.1.0
14 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API