分享

run Python on Android? Can I write native iPhone, android , windows ,blackberry apps using Python?

 quasiceo 2014-01-19

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 ?

asked Sep 19 '08 at 13:21
e-satis
129k55175235

2  
Let's test Joel's theory about the possibility of updates for well-google-ranked SO posts. See below or this: google-opensource./2009/06/… –  unmounted Jun 10 '09 at 5:14
70  
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 Jun 10 '09 at 8:17
9  
You mean not everybody is interested in python on android? –  unmounted Jun 10 '09 at 9:02
16  
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 Jun 10 '09 at 12:51
4  
@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 Feb 14 '12 at 15:39
show 5 more comments

19 Answers

up vote 239 down vote accepted

How about Kivy?
http://

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
https://play.google.com/store/apps/details?id=org.kivy.showcase

answered Nov 18 '11 at 21:49
JohnMudd
2,524186

8  
Wonderful. Something that's really innovative; –  e-satis Nov 19 '11 at 0:35
7  
i would be interested to hear about peoples experiances with this –  Hortinstein Dec 7 '11 at 22:48
6  
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 Dec 19 '11 at 13:39
12  
If you use Kivy, here is a tool to help package your project into an APK: github.com/kivy/python-for-android –  gdw2 Jan 9 '12 at 4:42
9  
Now, almost a full year later, is support any better? Has here been any notable improvements? –  TankorSmash Nov 17 '12 at 2:32
show 9 more comments

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

answered Jun 10 '09 at 5:24
Heat Miser
5,63572229

39  
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 Nov 26 '10 at 18:30
5  
@Stuart that cracked me up. --> normal user will be all "wtf is this ASE thing? –  sabertooth Dec 23 '10 at 5:50
14  
Further, ASE is a restricted environment; you cannot write full-blown Android apps even if ASE is pre-installed. See /questions/2076381 –  Sridhar Ratnakumar Jan 23 '11 at 21:36
2  
I think it was renamed to SL4A. –  Vanuan Dec 4 '12 at 21:25
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 Mar 9 '13 at 17:53
add comment

YES!

An example via Matt Cutts -- "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)
answered Jun 10 '09 at 5:13
unmounted
9,52563643


 
the curly quotes fsck with the code parsing –  lfaraone Dec 16 '09 at 0:59
14  
s/YES/meh. maybe/ ...it's extremely limited. anything graphical or multi touch? a big NO. –  gcb Oct 23 '10 at 9:09

 
@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 Mar 16 '12 at 16:10
3  
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 Dec 28 '12 at 6:20
add comment

"The Pygame Subset for Android 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.

answered Jan 28 '11 at 12:18
muriloq
88111424

1  
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 Oct 5 '11 at 21:56
add comment

There's also SL4A written by a Google employee.

answered May 26 '11 at 9:21
Rab Ross
717818

add comment

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:///blog/2011/09/cross-compiling-python-for-android/

EDIT: I've open sourced Ignifuga, 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).

answered Oct 12 '11 at 13:49
gabomdq
70757


 
Impressive. +1 for this. Not accepted because you can't possibly write anything for the public with this. –  e-satis Oct 13 '11 at 14:14

 
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 Oct 14 '11 at 2:38
add comment

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.

answered Nov 1 '08 at 20:29
lacker
2,04511728

35  
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 Nov 2 '08 at 18:20
42  
Hehe. 200 MHz... 4 years later and now phones have quad-core processors... LOL. –  Touzen Jul 26 '12 at 5:40

 
And still no Python. I love Java! –  Astral Projection Dec 31 '13 at 11:34
add comment

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

answered Dec 20 '08 at 16:56
Damon
33448

add comment

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.

answered Sep 19 '08 at 13:26
Ilya Kochetov
11k12743

add comment

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

http://www./?p=113

answered Dec 7 '10 at 21:46


 
Very nice. If you can run a server, you may be able to run django. Django demo on an android tablet anyone ? –  e-satis Dec 8 '10 at 11:02
1  
I've run web2py on my droid. Not too hard. –  gdw2 Oct 5 '11 at 21:55

 
@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 Sep 5 '12 at 20:12

 
@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 Sep 7 '12 at 4:32
add comment

SL4A 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 and works well, but Python2.6 is the best supported version.
  • You can also now do native Android user interfaces.
answered Mar 19 '12 at 15:45
Carl Smith
660317

add comment

PySide (Python QT Bindings) on android:

http:///2011/pyside-android/

answered Oct 5 '11 at 21:54
gdw2
1,048916

add comment

Using SL4A (which has already been mentioned by itself in other answers) you can run a full-blown web2py instance (other python web frameworks 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. 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.
answered Jan 6 '12 at 14:34
gdw2
1,048916


 
Cherrypy works well, with ws4py websocket support. Bottle is also fine on SL4A. –  Carl Smith Feb 13 '13 at 1:28

 
UPDATE: I've been using CherryPy and ws4py on SL4A with Python3 for a few weeks now, with no problems there either. –  Carl Smith Sep 19 '13 at 2:28
add comment

From the Python for android 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.

answered Jan 9 '12 at 4:46
gdw2
1,048916

1  
Duplicate of a duplicate. –  e-satis Jan 9 '12 at 8:22
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 Jan 9 '12 at 22:46

 
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 Jan 10 '12 at 0:55

 
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 Mar 16 '12 at 14:34
add comment

I use the qpython app, it has a editor, a console, and you can run your python programs with it, the app is free, here is the link http:///

answered Jun 12 '13 at 19:46
kyle k
4992217


 
Now qpython added Django support! That's all that I need! Amazing! –  swdev Oct 7 '13 at 14:36
add comment

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

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

answered May 9 '12 at 15:44
e-satis
129k55175235

add comment

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

You can learn sl4a Python Examples

answered Mar 11 '13 at 9:36
Hitul
267

add comment

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/

answered Sep 10 '12 at 5:38
Sahil Mahajan Mj
4,7592943

5  
Mentioned 4 times already. –  e-satis Sep 10 '12 at 8:26

 
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 Feb 13 '13 at 1:33
add comment

Yes you can use kivy

This is a nice cross platform python framework which works for Android, Win7, Linux, Mac. iOS is possibile but very trivial as Apple doesn't allow scripting.

And for Android Google provides something called ASE (Android Scripting Environment) which allows scripting languages (Python included) to run on Android. More details here

For iOS Python development would be to embed a Python interpreter into you app and distribute your Python script with it (so to play nicely with Apple rules). In this case your Python application would be a Python interpreter which is packaged with your script and runs it automatically. As I've said - it ain't pretty You can check details Here

Hope it helps you.

share|improve this answer


 
but for android at the end of application development ,testing will only allow in android(java) or unit testing (python) ? –  user2351396 May 5 '13 at 14:13

 
A better way is to compile down python code top C using cython and use it as a library. link to the C code from Obj-C –  MySchizoBuddy Aug 14 '13 at 6:40
add comment

Not out of the box. The final product needs to be compiled (iOS) or in bytecode (Android). Since it may be possible to convert Python code to Objective-C or something in between it might be possible.

See Kivi, the previous tread referenced it. This allows you to write you app in Python but probably uses a common wrapper, that Kivi provides, to run Pyton.

Please not that code written in Objective-C (iOS) or Java (Android) will always be faster than middleware like Kivi or Phonegap. Unless the one developing is very bad at writing any of those native languages....

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多