# Kotlin interop with Python (via C)

**URL:** https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774
**Category:** Native
**Created:** [March 20, 2020, 1:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774 "2020-03-20T13:21:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![aynum](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/aynum/32/5822_2.png) [@aynum](https://discuss.kotlinlang.org/u/aynum)
#### Post date: [March 20, 2020, 1:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/1 "2020-03-20T13:21:57Z")

</div>

I have some Kotlin code I’d like to re-use from a Python program/script. Is the best way to do this by using Kotlin Native to create a C library and then use that library from Python? I’ve looked at the Kotlin Native documentation on Github - and it looks like this should be possible.

1. is this the best approach?
2. if so are there some examples/documentation?

---

<div class="post-metadata">

### Author: ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)
#### Post date: [March 20, 2020, 2:15pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/2 "2020-03-20T14:15:25Z")

</div>

Hi there,

There is a [sample code](https://github.com/JetBrains/kotlin-native/tree/master/samples/python_extension) in the kotlin-native repository. Please note that the example is old and not up-to-date but you can see how it is supposed to work which is by creating a dynamic library ([doc here](https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html)) and then use the generated C headers to create a C bridge between Python and Kotlin.

This is not the best approach but it is the only one for now. As Python interop is clearly not one of the main use cases JetBrains is focused on I think you are on your own with this documentation-wise.

---

<div class="post-metadata">

### Author: ![guibbv2011](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@guibbv2011](https://discuss.kotlinlang.org/u/guibbv2011)
#### Post date: [March 20, 2020, 2:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/3 "2020-03-20T14:21:42Z")

</div>

hello aynum,

I don’t know if that can help you, you can call python a kotlin …

> **[kotlin-gradle-templates/embed-python at master ·...](https://github.com/dickensas/kotlin-gradle-templates/tree/master/embed-python)**
>
> master/embed-python

---

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [March 21, 2020, 6:09am UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/4 "2020-03-21T06:09:20Z")

</div>

Another solution is to try [GraalVM polyglot option](https://www.graalvm.org/docs/reference-manual/polyglot/). Never tried it myself, but it looks really promising.

---

<div class="post-metadata">

### Author: ![fleiber](https://avatars.discourse-cdn.com/v4/letter/f/4491bb/32.png) [@fleiber](https://discuss.kotlinlang.org/u/fleiber)
#### Post date: [March 27, 2020, 8:45pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/5 "2020-03-27T20:45:10Z")

</div>

We had the same need, which seems pretty natural (backend in Kotlin, user scripts in Python), and the most mature solution we found (not perfect yet though) is JPype: [JPype1 · PyPI](https://pypi.org/project/JPype1/)

---

<div class="post-metadata">

### Author: ![Martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Martmists](https://discuss.kotlinlang.org/u/Martmists)
#### Post date: [December 13, 2022, 9:26pm UTC](https://discuss.kotlinlang.org/t/kotlin-interop-with-python-via-c/16774/6 "2022-12-13T21:26:07Z")

</div>

I know the thread is nearly 3 years old at this point, but if anyone still needs this I’ve written a [Gradle Plugin for Kotlin/Multiplatform](https://github.com/Martmists-GH/kpy-plugin/) to target the Python C API. It currently only supports Python 3.10 on x86-64 however, as I don’t have a good way of building Windows targets for any version other than relying on msys2.
