I am making an app in kotlin. But I know python a lot and made the logic in python. The kotlin is only used for the display. Is there a way to call a python function in kotlin?. A python script can call python scripts but can a Kotlin script call one?
Asked
Active
Viewed 1.0k times
8
-
3See https://stackoverflow.com/questions/8898765/calling-python-in-java. Kotlin should be the same as Java for the purposes of this question (unless you are using Kotlin/JS, in which case look for calling Python from Javascript instead). – Alexey Romanov Jul 20 '20 at 08:20
-
I am using Kotlin as mentioned in my question. – GjdGr8 Jul 20 '20 at 08:32
-
Again, this won't make a difference. The answers for every JVM language will be the same. – Alexey Romanov Jul 20 '20 at 08:34
-
I found a post that says REPL helps. But there is not much information on the web. https://stackoverflow.com/questions/54021771/is-there-a-way-to-call-python-script-inside-of-a-kotlin-project – GjdGr8 Jul 20 '20 at 08:39
-
I couldn't find one for JVM – GjdGr8 Jul 20 '20 at 08:41
-
I am now very confused on what to do... – GjdGr8 Jul 20 '20 at 08:43
-
I am quite sure that answer is wrong and having a REPL doesn't help with calling Python in any way. – Alexey Romanov Jul 20 '20 at 08:44
-
Yes, REPL is not widely used too. – GjdGr8 Jul 25 '20 at 06:18
2 Answers
0
I think there are two normal solutions.
Using Polyglot API of GraalVM (https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html).
Creating a C interface implemented with Python (https://www.linuxjournal.com/article/8497) and calling it with JNI (https://docs.oracle.com/javase/8/docs/technotes/guides/jni/). However, this option needs you to manage your objects and memory very carefully (dispose all C pointers properly etc.)

Commander Tvis
- 2,244
- 2
- 15
- 41
-
1Foreign Function Interface (FFI) features are not for beginners. – Commander Tvis Jul 25 '20 at 13:32
-
1Consider using simple network protocols like HTTP or WebSocketts – Commander Tvis Jul 25 '20 at 13:33