share
Stack OverflowIs there any way to run Python on Android?
[+639] [18] e-satis
[2008-09-19 13:21:12]
[ python android ase android-scripting ]
[ http://stackoverflow.com/questions/101754/is-there-any-way-to-run-python-on-android ]

I like the Android platform. Actually, with some friends, we even participate to the ADC with the Spoxt project.

But Java is not my favourite language at all. We are working on a S60 version and this platform has a nice Python API. Of course there is nothing official about Python on Android, but since Jython exists, does anybody know a way to let the snake and the robot work together ?

(2) Let's test Joel's theory about the possibility of updates for well-google-ranked SO posts. See below or this: google-opensource.blogspot.com/2009/06/… - bvmou
(49) How is that post well ranked ? It's 4 lines long and there is like 3 answers ! I guess Pytho on Android has a huge sex appeal... - e-satis
(7) You mean not everybody is interested in python on android? - bvmou
(12) I mean the contrary. Given the little content of this post, if it's well ranked, it must be a huge expectation. I strongly hope the best for this project, I'm myself more a pythonista than a Java guy and coding Android with this language would sky rock the prototyping phase. - e-satis
I was kidding too. I meant that you should update the question, since some answers have been obsoleted by google's announcement. - bvmou
(2) Oh, right... I don't think updating the question is usefull, but changing the accepted answser certainly is. - e-satis
A scripting language inside a bytecode application inside a virtual machine it spell slowness. no wonder why those phones need dualcore even quadcore soon. Im not hating on Android or Python it just that... - user457015
Technically, when you code in Python, you always code using "a scripting language", it always generate bytecode, and it always run in a virtual machine. - e-satis
@e-satis you right. I'm new to python and its technicality, Im used to compiled languages C/C++. But still a virtual machine inside a virtual machine is not going to be the fastest thing ever, and can probably compete to be the slowest ever way to execute code. For sure its has its utility. I don't hate on Python nor Android, but ... - user457015
(2) @user457015 I think you should really read up on how modern JVMs are implemented, they're nowhere near to "scripting" or "interpretation" by now. If anything, Java is now only ~ 10-25% slower than well / perfectly written C/C++ code. And there's way less place to screw up writing in Java... - TC1
[+177] [2009-06-10 05:24:29] Heat Miser

There is also the new ASE project, it looks awesome, and has some integration with native Android components. Android Scripting Environment [1]

[1] http://www.talkandroid.com/1225-android-scripting-environment/

(38) True, but they have to have ASE installed, it's not a solution that lets you write an Android app in python without anything already installed (normal user will be all "wtf is this ASE thing?) - Stuart Axon
(3) @Stuart that cracked me up. --> normal user will be all "wtf is this ASE thing? - sabertooth
(14) Further, ASE is a restricted environment; you cannot write full-blown Android apps even if ASE is pre-installed. See stackoverflow.com/questions/2076381 - Sridhar Ratnakumar
(2) I think it was renamed to SL4A. - Vanuan
(2) You can write apps, package them, and even sell them on Play Store if you like, with SL4A now days. It's come along well since the comments above were posted. If you want Python on Android, then PY4A, which runs on SL4A is probably the best choice. - Carl Smith
1
[+162] [2011-11-18 21:49:45] JohnMudd [ACCEPTED]

How about Kivy?
http://kivy.org/#home

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

"Kivy is running on Linux, Windows, MacOSX and Android. You can run the same [python] code on all supported platforms."

Kivy Showcase app
https://market.android.com/details?id=org.kivy.showcase&hl=en [1]

[1] https://market.android.com/details?id=org.kivy.showcase&hl=en

(6) Wonderful. Something that's really innovative; - e-satis
(5) i would be interested to hear about peoples experiances with this - Hortinstein
(5) Works very nicely, builds somewhat large packages. But the source code is actually on the device, so you can inspect and edit on site if need be. - relet
(8) If you use Kivy, here is a tool to help package your project into an APK: github.com/kivy/python-for-android - gdw2
(4) @e-satis did Kivy work out for you? was it useful? I would be really thankful if you could post your experiences with Kivy in my question :) - julio.alegria
@julio Never had the chance to test it. We mostly do web app using jquery mobile instead. - e-satis
(3) I've been playing around with Kivy this past week attempting to write a game. Their main developers were very quick to answer questions on IRC however if you've programmed a GUI before Kivy will make you say WTF quite a bit. Some examples of undocumented things that were weird for me: All widgets get every on_touch_down event even if the event occurred outside their region, No widget has a draw() method, almost everything happens via a custom observer pattern on custom Properties they made up (note these share the name with Python's property, but are not the same) - Trey Stout
(3) @Trey: What about non-GUI-related things? Are there any limitations regarding usage of Python's modules? What about access to Android-specific features, such as ability to read messages, add notification, work in the background, make a photo, read contacts list, determine GPS location etc.? - Tadeck
(1) And now there is iOS support too! - rubik
(7) Now, almost a full year later, is support any better? Has here been any notable improvements? - TankorSmash
(1) And another year, damn google, with all the python they seem to love with websites, there's no love for python in Android. - Dexter
2
[+92] [2009-06-10 05:13:14] bvmou

YES! [1]

An example via Matt Cutts [2] -- "here’s a barcode scanner written in six lines of Python code:

import android
droid = android.Android()
code = droid.scanBarcode()
isbn = int(code['result']['SCAN_RESULT'])
url = "http://books.google.com?q=%d" % isbn
droid.startActivity('android.intent.action.VIEW', url)
[1] http://google-opensource.blogspot.com/2009/06/introducing-android-scripting.html
[2] http://www.mattcutts.com/blog/android-barcode-scanner/

the curly quotes fsck with the code parsing - lfaraone
(12) s/YES/meh. maybe/ ...it's extremely limited. anything graphical or multi touch? a big NO. - gcb
@gcb you can't use the normal android widget set, but you can use "webviews" (which is what the native gmail application uses, for example). - gdw2
(2) golfed: import android as a;d=a.Android();d.startActivity('android.intent.action.VIEW',"http://books.googl‌​e.com?q=%d"%int(d.scanBarcode()['result']['SCAN_RESULT'])) - Alex L
3
[+32] [2011-01-28 12:18:48] muriloq

"The Pygame Subset for Android [1] is a port of a subset of Pygame functionality to the Android platform. The goal of the project is to allow the creation of Android-specific games, and to ease the porting of games from PC-like platforms to Android."

The examples include a complete game packaged in an APK, which is pretty interesting.

[1] http://www.renpy.org/pygame/

Several aspects were broken on my Droid X (buttons, or touchscreen, can't remember), so I didn't get very far with this route. - gdw2
4
[+28] [2011-05-26 09:21:31] Rab Ross

There's also SL4A [1] written by a Google employee.

[1] http://code.google.com/p/android-scripting/

5
[+25] [2008-12-20 16:56:57] Damon

I just posted some directions for cross compiling Python 2.4.5 for Android [1]. It takes some patching, and not all modules are supported, but the basics are there.

[1] http://www.damonkohler.com/2008/12/python-on-android.html

6
[+23] [2008-11-01 20:29:45] lacker

As a python lover and Android programmer, I am sad to say this is not really a good way to go. There's two problems.

One problem is that there is a lot more than just a programming language to the Android development tools. A lot of the Android graphics involve XML files to configure the display, similar to HTML. The built-in java objects are really integrated with this XML layout, and it's a lot easier than writing your own code to go from logic to bitmap.

The other problem is that the G1 (and probably other android devices for the near future) are really not that fast. 200 Mhz processors, and RAM is very limited. Even in Java you have to do a decent amount of rewriting-to-avoid-more-object-creation if you want to make your app perfectly smooth. Python is going to be too slow for a while still on mobile devices.


(30) There is not a single java word on an Android phone, it's compiled to byte code during the packaging process. Speed is not the issue : Google could provide tools producing the right byte code from a python code (like for Jython). BTW, Dalvik is not the Java VM so this is not about Java VS Python. - e-satis
(19) Hehe. 200 MHz... 4 years later and now phones have quad-core processors... LOL. - Touzen
7
[+22] [2011-10-12 13:49:09] gabomdq

I've posted instructions and a patch for cross compiling Python 2.7.2 for Android, you can get it at my blog here: http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/

EDIT: I've open sourced Ignifuga [1], my 2D Game Engine, it's Python/SDL based and it cross compiles for Android. Even if you don't use it for games, you might get useful ideas from the code and the builder utility (named Schafer, after Tim...you know who).

[1] http://ignifuga.org

Impressive. +1 for this. Not accepted because you can't possibly write anything for the public with this. - e-satis
If you mean you can not do graphic apps with it, you most definitely can, of course, more work is needed. I actually use this port combined with SDL 1.3, it's not trivial to go from the python interpreter to an interactive app, but it can be done. - gabomdq
8
[+9] [2008-09-19 13:26:53] Ilya Kochetov

Not at the moment and you would be lucky to get Jython to work soon. If you're planning to start your development now you would be better off with just sticking to Java for now on.


9
[+8] [2010-12-07 21:46:18] android lover

Check out this blog here that explains how to install and run python and a simple webserver written in python on Android.

http://www.saffirecorp.com/?p=113


Very nice. If you can run a server, you may be able to run django. Django demo on an android tablet anyone ? - e-satis
(1) I've run web2py on my droid. Not too hard. - gdw2
@gdw2 im curious how you managed to get web2py to run here? using sl4a and using python to run webpy.py to start the server? When you do this though, you need to launch the browser separately. And im sure you would have to make the end users install sl4a as well. - luckysmack
@luckysmack If I recall, I only started it using the terminal (ssh'd in to my phone). I never went so far as to package it into an app (with its own icon). - gdw2
10
[+6] [2011-10-05 21:54:09] gdw2

PySide (Python QT Bindings) on android:

http://thp.io/2011/pyside-android/


11
[+6] [2012-03-19 15:45:28] Carl Smith

SL4A [1] does what you want and is actively developed. You can install it on your droid easily from their site, and don't need root.

It supports a range of languages, Python support is currently for version 2.6, but the owner, Robbie, is personally working on Python3.

SL4A scripts have a library available for each supported language that gives you an interface to the Android API as a single Android object:

import android
droid = android.Android()

# say "hello world" using the text to speech facade
droid.ttsSpeak('hello world')

Each language has the pretty much the same API, and you can even access the device with JavaScript inside a webview or something:

var droid = new Android();
droid.ttsSpeak('hello from js');

SL4A URL: http://code.google.com/p/android-scripting/

Update

  • Python3 is out, but Python2.6 is the best supported version.
  • You can also now do native Android user interfaces, but this stuff is especially experimental.
[1] http://code.google.com/p/android-scripting/

12
[+5] [2012-01-06 14:34:25] gdw2

Using SL4A [1] (which has already been mentioned by itself in other answers) you can run [2] a full-blown web2py [3] instance (other python web frameworks [4] are likely candidates as well). SL4A doesn't allow you to do native UI components (buttons, scroll bars, and the like), but it does support WebViews [5]. A WebView is basically nothing more than a striped down web browser pointed at a fixed address. I believe the native Gmail app uses a WebView instead of going the regular widget route.

This route would have some interesting features:

  • In the case of most python web frameworks, you could actually develop and test without using an android device or android emulator.
  • Whatever Python code you end up writing for the phone could also be put on a public webserver with very little (if any) modification.
  • You could take advantage of all of the crazy web stuff out there: query, HTML5, CSS3, etc.
[1] http://code.google.com/p/android-scripting/
[2] http://groups.google.com/group/web2py/browse_thread/thread/f227e93fe802a902
[3] http://web2py.com/
[4] http://wiki.python.org/moin/WebFrameworks
[5] http://code.google.com/p/android-scripting/wiki/UsingWebView

Cherrypy works well, with ws4py websocket support. Bottle is also fine on SL4A. - Carl Smith
13
[+5] [2012-01-09 04:46:54] gdw2

From the Python for android [1] site:

Python for android is a project to create your own Python distribution including the modules you want, and create an apk including python, libs, and your application.

[1] https://github.com/kivy/python-for-android

(1) Duplicate of a duplicate. - e-satis
(1) @e-satis: Thanks for your comment. I don't see which answer I'm duplicating. I can only guess that you think PFA is the same as Kivy. Though it's hosted under the Kivy project, you don't even have to use Kivy to use PFA. - gdw2
Check @tito's deleted answer at the bottom point to PFA. Plus, there is little interest is running PFA without kivy since it's the only toolkit you got. - e-satis
It's not because kivy is currently the only toolkit available (some people are working to intregrate others) than it's a duplicate. Please consider the others options as-it, and not merge all into one post :| - tito
14
[+3] [2012-05-09 15:44:49] e-satis

Yet another attempt: https://code.google.com/p/android-python27/

This one embed directly the Python interpretter in your app apk.


15
[+2] [2011-03-29 16:42:07] fooba

There's also python-on-a-chip possibly running mosync:

http://groups.google.com/group/python-on-a-chip/browse_thread/thread/df1c837bae2200f2/02992219b9c0003e?lnk=gst&q=mosync#02992219b9c0003e [1]

[1] http://groups.google.com/group/python-on-a-chip/browse_thread/thread/df1c837bae2200f2/02992219b9c0003e?lnk=gst&q=mosync#02992219b9c0003e

16
[+1] [2013-03-11 09:36:59] Hitul

You can run your python code using sl4a [1]. sl4a supports Python, Perl, JRuby, Lua, BeanShell, JavaScript, Tcl, and shell script.

You can learn sl4a Python Examples [2]

[1] http://code.google.com/p/android-scripting/
[2] http://code.google.com/p/android-scripting/wiki/Tutorials

17
[-4] [2012-09-10 05:38:01] Sahil Mahajan Mj

There is a wonderful project called the Scripting Layer for Android (SL4A) that is bringing scripting languages to the Android platform and providing a working alternative to Java development.

To get started, you need a copy of the Android SDK running on your computer, and you need a Java VM. Although you aren't going to program your Android app with Java, you still need a Java runtime upon which to execute the Android emulator, which is part of the SDK. The inclusion of the Android emulator gives you a sandboxed testbed to play in while you create your app.

Then you need to download and install Sl4A. You can download a copy from here. http://code.google.com/p/android-scripting/


(5) Mentioned 4 times already. - e-satis
You don't need to install anything on your computer. I think you're thinking of Python4Android27. SL4A can use ADB and other tools, because they're handy, but it works fully without them. - Carl Smith
18