Thursday, January 4, 2018

Running Python Script from Android Activity

Leave a Comment

I am collecting some data from my android application. How to run a python script in my android application that uses the collected data as input and generates some output?

2 Answers

Answers 1

Consider Jython, Jython is an implementation of the high-level, dynamic, object-oriented language Python seamlessly integrated with the Java platform. The predecessor to Jython, JPython, is certified as 100% Pure Java.

  1. mbedded scripting - Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.
  2. Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython.
  3. Rapid application development - Python programs are typically 2-10X shorter than the equivalent Java program. This translates directly to increased programmer productivity. The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.

The awesome features of Jython are as follows,

  • Dynamic compilation to Java bytecodes - leads to highest possible performance without sacrificing interactivity.
  • Ability to extend existing Java classes in Jython - allows effective use of abstract classes.

Jython doesn't compile to "pure java", it compiles to java bytecode subsequently to class files. To develop for Android, one compile java bytecode to Dalvik bytecode. To be honest, this path of development is not official , thus you will run into lots of problem with compatibility of course.

Answers 2

You may use qpython,

QPython is a script engine that runs Python on android devices. It lets your android device run Python scripts and projects. It contains the Python interpreter, console, editor, and the SL4A Library for Android. It’s Python on Android! It offers the development kit which lets you easily develop Python projects and scripts on your Android device.

If you need to add some specifics libraries, according to qpython documentation install-libraries :

  • You can install most pure python libraries through pip_console.py which included from the 0.9.8.2 version

You may add --no-use-wheel argument when using pip_console.py to avoid unknown archive format :whl issue when installing some package.

and / or

  • You can upload your libraries into the /sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages or /sdcard/com.hipipal.qpyplus/lib/python3.2/site-packages (For QPython3)

Regards

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment