How to Run Python on Android

Is there a way to run Python on Android?

One way is to use Kivy:

Open source Python library for rapid development of applications
that make use of innovative user interfaces, such as multi-touch apps.

Kivy runs on Linux, Windows, OS X, Android and iOS. You can run the same [python] code on all supported platforms.

Kivy Showcase app

How to run python script in Android studio using chaquopy?

It is showing no module named random found.

I assume you're talking about an error shown in the Android Studio editor, rather than one which happened at runtime. As the documentation says, errors in the editor are harmless: just go ahead and run your app, and if there really is an error, the details will be displayed in the Logcat.

How can I get the list of keys and values from python script?

You can write the Python code like this:

return [f'{key}: {name}' for key in players for name in players[key]]

And the Java code like this:

file.callAttr("getPlayers", wk, batsman, bowler, ar).toJava(String[].class)

That will give you a Java String[] array, which you can then process however you want.


EDIT for jarray does not support slice syntax:

This issue is fixed in Chaquopy 9.0.0. With older versions, you can work around it by converting the array to a Python list.

To convert in Python:

def getPlayers(wk, batsman, bowler, allRounder):
wk = list(wk)
batsman = list(batsman)
# etc.

Or to convert in Java:

PyObject list = python.getBuiltins().get("list");
return file.callAttr("getPlayers",
list.call(wk.toArray()),
list.call(batsman.toArray()),
// etc.

Running Python scripts in Java class activity in Android Studio with Chaquopy

I've heard from quite a few people who've used face_recognition successfully on Android. It's a pure-Python package, so it can be installed directly from PyPI and doesn't need to be in Chaquopy's own package repository.

However, Chaquopy doesn't come with a Python executable, so running a command-line script with os.system is unlikely to work. Instead, you can just call the face_recognition Python API, which has many examples on its own website.

It looks like the closest equivalent to running the face_recognition script is to import face_recognition.face_recognition_cli and call the main function, possibly after setting up sys.argv with your command line.

Any text printed to standard output will go to Logcat, which you can view in Android Studio. If you'd like to see it on the device screen as well, the easiest way is to adapt the console app template.

How to run Python code made for Android device on Windows 10?

I found out that Android Studio has a built in Android Device Emulator, wich is basically a virtual Android device. Inside that you can download Pydroid 3 and run your Python code there. If your application requires addiditional permissions, you can use Pydroid Additional Permissions Plugin.



Related Topics



Leave a reply



Submit