pvieito/PythonKit
pvieito/PythonKit has 2,231 GitHub stars and 247 forks. GitHot measured +1 stars in 24 hours. Last checked 2026-11-07 22:12 UTC. Last push 2026-10-23.
Swift framework to interact with Python.
Measured star growth
- in 24h
- +1
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalPythonKit
Swift framework to interact with Python.
Usage
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()}")
The same code can be implemented in Swift through PythonKit:
import PythonKit
let sys = 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", from: "1.0.0"),
Environment Variables
As Python libraries 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 a 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:
$ PYTHON_LOADER_LOGGING=TRUE PYTHON_VERSION=3.8 swift run
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.
Troubleshooting
- If you 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.
Notes
- Originally PythonKit was based on the
Pythonmodule from the Swift for TensorFlow experimental project. - If you have questions about PythonKit you can ask on the Swift Forums.
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
560,819 starspublic-apis/public-apis
A collective list of free APIs
497,054 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
441,335 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,154 stars