问题:有没有办法在Android上运行Python?

我们正在开发S60版本,该平台具有不错的Python API。

但是,关于Android上的Python尚无官方资料,但是由于Jython存在,有没有办法让蛇和机器人一起工作?

We are working on an S60 version and this platform has a nice Python API..

However, there is nothing official about Python on Android, but since Jython exists, is there a way to let the snake and the robot work together??


回答 0

一种方法是使用Kivy

开源Python库,用于快速开发利用创新用户界面的应用程序,例如多点触控应用程序。

Kivy可在Linux,Windows,OS X,Android和iOS上运行。您可以在所有受支持的平台上运行相同的[python]代码。

Kivy Showcase应用程序

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


回答 1

还有一个新的Android脚本环境(ASE / SL4A)项目。它看起来很棒,并且与本机Android组件集成在一起。

注意:不再处于“主动开发”之下,但是可能有一些分支。

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

Note: no longer under “active development”, but some forks may be.


回答 2

是! :Android脚本环境

一个例子通过马特·卡茨通过SL4A – “这是写在Python代码半年线条码扫描器:

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)

Yes! : Android Scripting Environment

An example via Matt Cutts via SL4A — “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)

回答 3

适用于Android的Pygame子集

Pygame是适用于Python(在桌面上)的2D游戏引擎,在新程序员中很流行。AndroidPygame子集将自己描述为…

…将Pygame功能的子集移植到Android平台。该项目的目标是允许创建特定于Android的游戏,并简化游戏从类似PC的平台到Android的移植。

示例包括打包为APK的完整游戏,这很有趣。

Pygame Subset for Android

Pygame is a 2D game engine for Python (on desktop) that is popular with new programmers. The Pygame Subset for Android describes itself as…

…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 as an APK, which is pretty interesting.


回答 4

交叉编译和Ignifuga

我的博客上有说明和补丁用于Android的Python 2.7.2交叉编译的。

我还开源了我的2D游戏引擎Ignifuga。它基于Python / SDL,并且可以为Android交叉编译。即使您不将它用于游戏,您也可能会从代码或构建器实用程序(以Tim 命名的Schafer ……知道谁)中得到有用的想法。

Cross-Compilation & Ignifuga

My blog has instructions and a patch for cross compiling Python 2.7.2 for Android.

I’ve also 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 or builder utility (named Schafer, after Tim… you know who).


回答 5

Android脚本层

SL4A做您想要的。您可以轻松地将其从其站点直接安装到设备上,而无需root用户。

它支持多种语言。Python是最成熟的。默认情况下,它使用Python 2.6,但是您可以使用3.2端口。我已经将该端口用于Galaxy S2上的所有东西,并且工作正常。

API

SL4A android为每种支持的语言提供了其库的端口。该库通过单个Android对象提供了与基础Android API的接口。

from android import Android

droid = Android()
droid.ttsSpeak('hello world') # example using the text to speech facade

每种语言都有几乎相同的API。您甚至可以在webview中使用JavaScript API。

let droid = new Android();
droid.ttsSpeak("hello from js");

使用者介面

对于用户界面,您有三个选择:

  • 您可以通过API轻松使用通用的本机对话和菜单。这对于确认对话和其他基本用户输入很有用。
  • 您还可以从Python脚本中打开Web视图,然后将HTML5用作用户界面。当您使用Python中的Web视图时,可以在Web视图和生成它的Python进程之间来回传递消息。用户界面不会是本机的,但仍然是一个不错的选择。
  • 一些对Android本机用户界面的支持,但是我不确定它的运行情况如何;我只是从未使用过它。

您可以混合使用选项,因此您可以在主界面上拥有一个Web视图,并且仍然使用本机对话。

QPython的

有一个名为QPython的第三方项目。它建立在SL4A之上,并抛出了其他有用的东西。

QPython为您提供了一个更好的UI来管理安装,并包括一个小的触摸屏代码编辑器,一个Python Shell和一个用于程序包管理的PIP Shell。它们还具有Python 3端口。两种版本均可从Play商店免费获得。QPython还在Android项目(包括Kivy)上捆绑了来自大量Python的库,因此它不仅仅是SL4A。

请注意,QPython仍在开发SL4A的分支(尽管,说实话,没有太多)。SL4A的主要项目本身已经死了。

有用的链接

Scripting Layer for Android

SL4A does what you want. You can easily install it directly onto your device from their site, and do not need root.

It supports a range of languages. Python is the most mature. By default, it uses Python 2.6, but there is a 3.2 port you can use instead. I have used that port for all kinds of things on a Galaxy S2 and it worked fine.

API

SL4A provides a port of their android library for each supported language. The library provides an interface to the underlying Android API through a single Android object.

from android import Android

droid = Android()
droid.ttsSpeak('hello world') # example using the text to speech facade

Each language has pretty much the same API. You can even use the JavaScript API inside webviews.

let droid = new Android();
droid.ttsSpeak("hello from js");

User Interfaces

For user interfaces, you have three options:

  • You can easily use the generic, native dialogues and menus through the API. This is good for confirmation dialogues and other basic user inputs.
  • You can also open a webview from inside a Python script, then use HTML5 for the user interface. When you use webviews from Python, you can pass messages back and forth, between the webview and the Python process that spawned it. The UI will not be native, but it is still a good option to have.
  • There is some support for native Android user interfaces, but I am not sure how well it works; I just haven’t ever used it.

You can mix options, so you can have a webview for the main interface, and still use native dialogues.

QPython

There is a third party project named QPython. It builds on SL4A, and throws in some other useful stuff.

QPython gives you a nicer UI to manage your installation, and includes a little, touchscreen code editor, a Python shell, and a PIP shell for package management. They also have a Python 3 port. Both versions are available from the Play Store, free of charge. QPython also bundles libraries from a bunch of Python on Android projects, including Kivy, so it is not just SL4A.

Note that QPython still develop their fork of SL4A (though, not much to be honest). The main SL4A project itself is pretty much dead.

Useful Links


回答 6

作为一个Python的爱好者和Android程序员,我很伤心地说,这是不是一个很好的路要走。有两个问题:

一个问题是,Android开发工具不只是一种编程语言。许多Android图形涉及XML文件来配置显示,类似于HTML。内置的Java对象与此XML布局集成在一起,比编写代码从逻辑到位图要容易得多。

另一个问题是G1(以及不久的将来可能还有其他Android设备)的运行速度并不快。200 MHz处理器和RAM非常有限。即使是在Java中,如果要使应用程序完全流畅,也必须进行大量重写以避免创建更多对象。在移动设备上运行一段时间后,Python将变得太慢。

As a Python lover and Android programmer, I’m sad to say this is not a good way to go. There are 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 integrated with this XML layout, and it’s a lot easier than writing your code to go from logic to bitmap.

The other problem is that the G1 (and probably other Android devices for the near future) are 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.


回答 7

基维

我想补充一下@JohnMudd关于Kivy的文章。自从他描述这种情况以来已经有好几年了,而Kivy有了长足的发展。

我认为,Kivy的最大卖点是其跨平台兼容性。您可以使用任何桌面环境(Windows / * nix等)对所有内容进行编码和测试,然后将您的应用打包到一系列不同的平台上,包括Android,iOS,MacOS和Windows(尽管应用通常缺乏本机外观)。

使用Kivy自己的KV语言,您可以轻松编码和构建GUI界面(就像Java XML一样,但不是TextView等,KV拥有自己的ui.widgets的类似翻译功能),我认为这很容易采用。

当前,最推荐使用Buildozerpython-for-android工具来构建和打包应用程序。我对它们都进行了尝试,可以肯定地说,它们使使用Python构建Android应用程序变得轻而易举。他们的指南也有据可查。

iOS是Kivy的另一个大卖点。您可以使用相同的代码库,通过kivy-ios Homebrew工具进行少量更改,尽管构建需要Xcode,但在其设备上运行之前(AFAIK,Xcode中的iOS Simulator当前不适用于x86体系结构构建) 。为了成功构建,还必须手动在Xcode中解决一些依赖项问题,并加以解决,但这并不太容易解决,并且Kivy Google Group的人员也非常有帮助。

综上所述,具有良好Python知识的用户应该不会有任何问题,很快就能掌握基础知识。

如果将Kivy用于更重要的项目,则可能会发现现有模块不令人满意。虽然有一些可行的解决方案。通过适用于Android的pyjniuspyobjus,用户现在可以访问Java / Objective-C类来控制某些本机API。

Kivy

I wanted to add to what @JohnMudd has written about Kivy. It has been years since the situation he described, and Kivy has evolved substantially.

The biggest selling point of Kivy, in my opinion, is its cross-platform compatibility. You can code and test everything using any desktop environment (Windows/*nix etc.), then package your app for a range of different platforms, including Android, iOS, MacOS and Windows (though apps often lack the native look and feel).

With Kivy’s own KV language, you can code and build the GUI interface easily (it’s just like Java XML, but rather than TextView etc., KV has its own ui.widgets for a similar translation), which is in my opinion quite easy to adopt.

Currently Buildozer and python-for-android are the most recommended tools to build and package your apps. I have tried them both and can firmly say that they make building Android apps with Python a breeze. Their guides are well documented too.

iOS is another big selling point of Kivy. You can use the same code base with few changes required via kivy-ios Homebrew tools, although Xcode is required for the build, before running on their devices (AFAIK the iOS Simulator in Xcode currently doesn’t work for the x86-architecture build). There are also some dependency issues which must be manually compiled and fiddled around with in Xcode to have a successful build, but they wouldn’t be too difficult to resolve and people in Kivy Google Group are really helpful too.

With all that being said, users with good Python knowledge should have no problem picking up the basics quickly.

If you are using Kivy for more serious projects, you may find existing modules unsatisfactory. There are some workable solutions though. With the (work in progress) pyjnius for Android, and pyobjus, users can now access Java/Objective-C classes to control some of the native APIs.


回答 8

Termux

您可以使用Termux应用程序(该程序为Android提供POSIX环境)来安装Python。

请注意,这apt install python将在Termux上安装Python3。对于Python2,您需要使用apt install python2

Termux

You can use the Termux app, which provides a POSIX environment for Android, to install Python.

Note that apt install python will install Python3 on Termux. For Python2, you need to use apt install python2.


回答 9

目前还没有,您很幸运能让Jython很快上班。如果您打算现在开始开发,那么最好还是坚持使用Java。

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.


回答 10

使用SL4A(在其他答案中已经提到过),您可以运行成熟的web2py实例(其他python web框架也可能是候选对象)。SL4A不允许您执行本机UI组件(按钮,滚动条等),但它确实支持WebViews。WebView基本上只不过是指向固定地址的带状结构的Web浏览器。我相信本机Gmail应用程序使用WebView而非常规的窗口小部件路线。

这条路线将具有一些有趣的功能:

  • 对于大多数python网络框架,您实际上可以在不使用android设备或android模拟器的情况下进行开发和测试。
  • 无论您最终为手机编写的任何Python代码,也都可以进行很小的修改(如果有的话)放在公共Web服务器上。
  • 您可以利用那里所有疯狂的网络内容:查询,HTML5,CSS3等。

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.

回答 11

QPython的

我使用QPython应用程序。它是免费的,包括代码编辑器,交互式解释器和程序包管理器,可让您直接在设备上创建和执行Python程序。

QPython

I use the QPython app. It’s free and includes a code editor, an interactive interpreter and a package manager, allowing you to create and execute Python programs directly on your device.


回答 12

适用于AndroidPython站点:

适用于Android的Python是一个用于创建自己的Python发行版(包括所需模块)并创建包含python,lib和应用程序的apk的项目。

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.


回答 13

Chaquopy

Chaquopy是Android Studio基于Gradle的构建系统的插件。它着重于与标准Android开发工具的紧密集成

  • 它提供了完整的API,可以从Python调用Java从Java调用Python,从而允许开发人员使用最适合其应用程序每个组件的语言。

  • 它可以自动下载PyPI软件包并将其构建到应用程序中,包括选定的本机软件包,例如NumPy。

  • 它使您能够从Python完全访问所有Android API,包括本机用户界面工具包(纯Python活动示例)。

这是一种商业产品,但可免费用于开放源代码,并且将始终保持这种状态。

(我是这个产品的创造者。)

Chaquopy

Chaquopy is a plugin for Android Studio’s Gradle-based build system. It focuses on close integration with the standard Android development tools.

  • It provides complete APIs to call Java from Python or Python from Java, allowing the developer to use whichever language is best for each component of their app.

  • It can automatically download PyPI packages and build them into an app, including selected native packages such as NumPy.

  • It enables full access to all Android APIs from Python, including the native user interface toolkit (example pure-Python activity).

This is a commercial product, but it’s free for open-source use and will always remain that way.

(I am the creator of this product.)


回答 14

另一尝试:https : //code.google.com/p/android-python27/

这直接将Python解释器嵌入到您的应用apk中。

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

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


回答 15

是python官方网站上列出的一些工具


Playstore中有一个名为QPython3的应用程序,可用于编辑和运行python脚本。

Playstore连结


另一个名为Termux的应用程序,您可以在其中使用命令安装python

pkg install python

Playstore连结


如果您想开发应用程序,则可以使用Python Android脚本层(SL4A

The Scripting Layer for Android, SL4A, is an open source application that allows programs written in a range of interpreted languages to run on Android. It also provides a high level API that allows these programs to interact with the Android device, making it easy to do stuff like accessing sensor data, sending an SMS, rendering user interfaces and so on.


您还可以检查适用于Android的PySide,它实际上是Qt 4的Python绑定。


有一个称为PyMob的平台,其中的应用程序可以完全用Python编写,并且编译器工具流(PyMob)可以将它们转换为适用于各种平台的本机源代码。


同时检查python-for-android

python-for-android is an open source build tool to let you package Python code into standalone android APKs. These can be passed around, installed, or uploaded to marketplaces such as the Play Store just like any other Android app. This tool was originally developed for the Kivy cross-platform graphical framework, but now supports multiple bootstraps and can be easily extended to package other types of Python apps for Android.


试用适用 于Android的Chaquopy A Python SDK


Anddd … BeeWare

BeeWare allows you to write your app in Python and release it on multiple platforms. No need to rewrite the app in multiple programming languages. It means no issues with build tools, environments, compatibility, etc.

Here are some tools listed in official python website


There is an app called QPython3 in playstore which can be used for both editing and running python script.

Playstore link


Another app called Termux in which you can install python using command

pkg install python

Playstore Link


If you want develop apps , there is Python Android Scripting Layer (SL4A) .

The Scripting Layer for Android, SL4A, is an open source application that allows programs written in a range of interpreted languages to run on Android. It also provides a high level API that allows these programs to interact with the Android device, making it easy to do stuff like accessing sensor data, sending an SMS, rendering user interfaces and so on.


You can also check PySide for Android, which is actually Python bindings for the Qt 4.


There’s a platform called PyMob where apps can be written purely in Python and the compiler tool-flow (PyMob) converts them in native source codes for various platforms.


Also check python-for-android

python-for-android is an open source build tool to let you package Python code into standalone android APKs. These can be passed around, installed, or uploaded to marketplaces such as the Play Store just like any other Android app. This tool was originally developed for the Kivy cross-platform graphical framework, but now supports multiple bootstraps and can be easily extended to package other types of Python apps for Android.


Try Chaquopy A Python SDK for Android


Anddd… BeeWare

BeeWare allows you to write your app in Python and release it on multiple platforms. No need to rewrite the app in multiple programming languages. It means no issues with build tools, environments, compatibility, etc.


回答 16

您可以使用sl4a运行Python代码。sl4a支持Python,PerlJRubyLua,BeanShell,JavaScript,Tcl和Shell脚本。

您可以学习sl4a Python示例

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.


回答 17

您可以使用QPython

它具有Python控制台,编辑器以及程序包管理/安装程序

http://qpython.com/

这是一个具有Python 2和Python 3实现的开源项目。您可以直接从github下载源代码和Android .apk文件。

QPython 2:https//github.com/qpython-android/qpython/releases

QPython 3:https//github.com/qpython-android/qpython3/releases

You can use QPython:

It has a Python Console, Editor, as well as Package Management / Installers

http://qpython.com/

It’s an open source project with both Python 2 and Python 3 implementations. You can download the source and the Android .apk files directly from github.

QPython 2: https://github.com/qpython-android/qpython/releases

QPython 3: https://github.com/qpython-android/qpython3/releases


回答 18

如果您正在寻找3.4.2或3.5.1,则另一个选择是GitHub上的此存档。

Python3-Android 3.4.2 Python3-Android 3.5.1

目前,它支持Python 3.4.2或3.5.1以及NDK的10d版本。它还可以支持3.3和9c,11c和12

只需下载,运行make并获得.so或.a即可,这非常不错。

我目前使用它在Android设备上运行原始Python。通过对构建文件进行一些修改,您还可以将x86和armeabi制作为64位

Another option if you are looking for 3.4.2 or 3.5.1 is this archive on GitHub.

Python3-Android 3.4.2 or Python3-Android 3.5.1

It currently supports Python 3.4.2 or 3.5.1 and the 10d version of the NDK. It can also support 3.3 and 9c, 11c and 12

It’s nice in that you simply download it, run make and you get the .so or the .a

I currently use this to run raw Python on android devices. With a couple modifications to the build files you can also make x86 and armeabi 64 bit


回答 19

没有在此处看到此消息,但是由于Necessitas,Qt可以在Android上运行,因此您可以使用Pyside和Qt来做到这一点。

目前看来似乎很仓促,但最终可能是一条可行的路线…

http://qt-project.org/wiki/PySide_for_Android_guide

Didn’t see this posted here, but you can do it with Pyside and Qt now that Qt works on Android thanks to Necessitas.

It seems like quite a kludge at the moment but could be a viable route eventually…

http://qt-project.org/wiki/PySide_for_Android_guide


回答 20

pyqtdeploy似乎是一种选择,引用该文档是:

该工具与Qt随附的其他工具结合使用,可以部署用Python v2.7或Python v3.3或更高版本编写的PyQt4和PyQt5应用程序。它支持部署到桌面平台(Linux,Windows和OS X)以及移动平台(iOS和Android)。

根据通过pyqtdeploy和Qt5PyQt5应用程序部署到Android的说法,该应用程序是积极开发的,尽管很难找到有效的Android应用程序示例或关于如何将所有必需的库交叉编译到Android的教程。请记住,这是一个有趣的项目!

One more option seems to be pyqtdeploy which citing the docs is:

a tool that, in conjunction with other tools provided with Qt, enables the deployment of PyQt4 and PyQt5 applications written with Python v2.7 or Python v3.3 or later. It supports deployment to desktop platforms (Linux, Windows and OS X) and to mobile platforms (iOS and Android).

According to Deploying PyQt5 application to Android via pyqtdeploy and Qt5 it is actively developed, although it is difficult to find examples of working Android apps or tutorial on how to cross-compile all the required libraries to Android. It is an interesting project to keep in mind though!


回答 21

看一下BeeWare。在回答这个问题时,它仍处于早期开发中。目的是能够使用Python为所有受支持的操作系统(包括Android)创建本机应用程序。

Take a look at BeeWare. At the moment of answering this question it is still in early development. It’s aim is to be able to create native apps with Python for all supported operating systems, including Android.


回答 22

看看enaml-native,它采用了react-native概念并将其应用于python。

它允许用户使用本机Android小部件来构建应用,并提供API以使用来自python的android和java库。

它还与android-studio集成,并共享了react的一些不错的开发功能,例如代码重载和远程调试。

Check out enaml-native which takes the react-native concept and applies it to python.

It lets users build apps with native Android widgets and provides APIs to use android and java libraries from python.

It also integrates with android-studio and shares a few of react’s nice dev features like code reloading and remote debugging.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。