- Notifications
You must be signed in to change notification settings - Fork0
Allows embedding a full CPython environment inside of a Kotlin JVM application
License
Mylo-Softworks/KPython
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Embedcpython inside ofKotlin/JVM applications, usingJNA.
KPython is an early project. Expect bugs.
Bugs should be reported in theissues section of this project.
By using CPython's api through JNA, this allows us to embed a full CPython interpreter in our Kotlin/JVM projects.
KPython uses CPython through its library, for python 3.12, this is python312.dll on Windows, and libpython3.12.so on Linux.
- Run Python code from Kotlin
- Give Kotlin code to Python as a function
- Convert Python types to Kotlin
- Convert Kotlin types to Python
- Add a Kotlin "Frontend" to a python backend, for use with python libraries such as pytorch, tensorflow, numpy, etc.
- Proxied access to python objects using
PythonProxyObjectinstances. - Proxied type-safe access to python objects using interfaces extending
KPythonProxy.
Python objects are represented asPythonProxyObject, attributes can be get and set with Kotlin's indexing. Functions/methods can be called usingPythonProxyObject.invokeMethod(name, args)
Mid-level proxies can be converted to specific High-level proxies.
High-level proxies are Java Proxy interfaces which extendKPythonProxy, which will be automatically implemented at runtime.
High-level proxies can access the Mid-level proxy they're based on.
Return and input types are automatically converted if possible.
see more
Some types can be converted automatically between Kotlin and Python, these types include:
- Byte, Short, Int, Long (Represented as long in python)
- Float, Double (Represented as double in python)
- String
- Array -> list (Exclusively Array, not List or ArrayList, as those are type-erased)
- (To python only) HashMap -> dict
- PythonProxyObject -> object (No conversion occurs, as PythonProxyObject holds the real representation of the proxy)
- KPythonProxy -> object (Converted to PythonProxyObject, then use the value)
Kotlin functions are used to execute kotlin code from python, no automatic conversion exists, but there are many ways to create them.
see more
A few methods exist to support relative imports from python. Getting and setting argv is also supported.PyEnvironment.setFakeFileDir() is used to set the directory where imports come from. Optionally, a__file__ can also be set for the PyEnvironment.PyEnvironment.setArgv() is used to set argv, as if the script was ran from a file with arguments.setArgv takes a string for the filename, and thenvarargs String for the arguments.
Creating an environment is done likeval env = PyEnvironment(PythonVersion.python312). More documentation pending.
About
Allows embedding a full CPython environment inside of a Kotlin JVM application
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.