Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Swift framework to interact with Python.

License

NotificationsYou must be signed in to change notification settings

pvieito/PythonKit

Repository files navigation

Swift framework to interact with Python.

Usage

Some Python code like this:

importsysprint(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 PythonKitletsys=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 yourPackage.swift manifest:

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

Environment Variables

As the Python library are loaded at runtime byPythonKit, it will try to find the most modern Python version available in the system. You can force a given version with thePYTHON_VERSION environment variable or an specific Python library path or name withPYTHON_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

IfPythonKit cannot find and load the Python library you can set thePYTHON_LOADER_LOGGING environment variable to know from which locationsPythonKit is trying to load the library:

$ PYTHON_LOADER_LOGGING=TRUE PYTHON_VERSION=3.8 swift runLoading 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 your are targeting the Mac platform with theHardened Runtime enabled make sure you are properly signing and embedding the Python framework you are trying to load withPythonKit. The Hardened RuntimeLibrary Validation mechanism prevents a process from loading libraries that are not signed by Apple or the same developer as the main process.

Notes

  • OriginallyPythonKit was based on thePython module from theSwift for TensorFlow experimental project.
  • If you have questions aboutPythonKit you can ask on theSwift Forums.

About

Swift framework to interact with Python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors13

Languages


[8]ページ先頭

©2009-2025 Movatter.jp