如何在Android设备上运行Python脚本及带多依赖库的Python应用?
Hey there! Running Python scripts (and even full apps with dependencies) on Android is totally doable—here are the most reliable methods I’ve tested and recommend:
Termux is a terminal emulator for Android that gives you a full Linux-like environment, perfect for running Python with all your dependencies. Here’s how to set it up:
- 首先从F-Droid安装Termux(Google Play上的版本比较旧,功能不全)
- 打开Termux后,先更新包管理器:
pkg update && pkg upgrade - 安装Python:
验证安装成功:pkg install pythonpython --version - 导入你的Python应用和依赖文件:你可以通过Termux的共享文件夹(在Android的文件管理器里找
Termux文件夹)上传文件,或者用云盘下载,甚至用scp从电脑传过来 - 安装依赖:如果你的应用有
requirements.txt,导航到文件所在目录,运行:pip install -r requirements.txt - 运行脚本:进入脚本目录,执行:
python your_script.py - 额外提示:如果你的应用需要图形界面,你可以安装Termux的X11转发工具,或者直接用支持GUI的库(比如Tkinter,Termux里可以通过
pkg install python-tkinter安装)
If you want a more user-friendly, IDE-based approach, Pydroid 3 is a great choice—it’s a full Python IDE built for Android:
- 从Google Play或F-Droid安装Pydroid 3
- 打开后,它自带一个预配置的Python环境,你可以直接新建脚本,或者通过“Import”按钮导入本地的脚本文件
- 安装依赖:打开Pydroid的内置终端,用
pip install 依赖包名安装单个依赖,或者上传requirements.txt到Pydroid的文件目录后,运行:pip install -r requirements.txt - 运行应用:直接点击IDE顶部的运行按钮就行,它支持大部分常用纯Python库(比如requests、numpy、pandas都能轻松安装)
- 注意:如果你的依赖包含需要编译的C扩展,部分库可能无法在Pydroid上安装,这种情况下Termux会更靠谱
If you want to turn your Python app into a standalone APK that anyone can install without needing a Python environment, these tools work great:
Kivy(适合跨平台GUI应用)
Kivy is a Python framework for building cross-platform GUI apps, and it can be packaged into Android APKs easily:
- 在电脑上安装Kivy和Buildozer:
pip install kivy buildozer - 进入你的Python项目目录,初始化Buildozer配置:
buildozer init - 编辑生成的
buildozer.spec文件:设置你的应用名称(package.name)、域名(package.domain),并在requirements字段里列出所有依赖(比如python3,kivy,requests) - 开始打包:
Buildozer会自动下载Android SDK、NDK和所有需要的依赖,编译完成后,你可以在项目的buildozer android debugbin目录找到生成的APK,传到Android设备安装即可
BeeWare(原生风格Android应用)
BeeWare lets you build native-looking Android apps with Python. The setup is a bit more involved, but the end result feels like a native Android app. You’ll need to follow their official setup guide to install the toolchain, then use commands like briefcase create and briefcase build android to package your app.
- 存储权限:不管用哪种方法,记得给应用授予存储权限,这样才能读取本地的脚本和依赖文件
- 性能:Android设备的CPU/GPU性能通常不如桌面电脑,计算密集型的脚本可能运行较慢
- C扩展依赖:Termux和Buildozer都有自己的编译环境,能处理大部分带C扩展的依赖,但Pydroid对这类支持有限
内容的提问来源于stack exchange,提问作者Andres Sosa




