Mrakorez/py2droid
关于
Magisk/KernelSU/APatch module to install Python 3 on Android.
开发工具实测星标增长
- 24小时
- +1
- 7天
- +2
- 15天
- 正在收集历史
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文What is Py2Droid?
Py2Droid is CPython 3 compiled for Android and packaged as a Magisk module. It provides system-level Python access without requiring Termux or other userland environments.
Built following official Python for Android guidelines, stripped of unnecessary components for minimal size.
Project Status
This project is in pre-1.0 development.
Current focus has moved to other projects, so updates here are mostly limited to keeping CPython builds fresh and fixing critical bugs on a spare-time basis. Breaking changes may land in minor releases.
In the future, I plan to rewrite the build and repository automation scripts to make the codebase cleaner and improve Py2Droid for module developers. However, since this was one of my first projects where I gained hands-on Python experience, the codebase might be pretty terrible in places for now :D
Quick Start
- Download the latest release from Releases
- Install via your root manager (Magisk/KernelSU/APatch)
- Reboot your device
- Start using Python:
python3 --version pip3 install --user requests
Why Py2Droid?
For Magisk Module Developers
Use Python instead of shell scripts in your modules. Python is available as a dependency or runtime for complex logic.
For System Scripts
Get lightweight Python access at boot time without Termux overhead. Perfect for automation scripts and utilities.
vs Termux
| Feature | Py2Droid | Termux Python |
|---|---|---|
| Version | Latest stable Python 3 | May lag behind |
| Installation | Single module | App + packages |
| Size (aarch64) | ~44 MB installed | ~60 MB installed |
| Boot-time access | ✅ Available during early boot | ❌ Unavailable until unlock |
| Location | /data/adb (always accessible) |
/data/user (encrypted) |
| Use case | System scripts, Magisk modules | Full development environment |
Note: Py2Droid is not a Termux replacement. They serve different purposes.
System Requirements
- Android: 5.0+
- Architecture: ARM64 or x86_64
- Root: Magisk/KernelSU/APatch
Limitations
Py2Droid runs in a bare Android environment (device with minimal shell, without Linux userland tools):
- No GUI support - No X11/Wayland, so no Tkinter
- Limited native extensions - Most packages with C/C++/Rust code don’t provide Android wheels, and you can’t compile them on-device
- Some stdlib modules unavailable - Modules requiring missing system libraries may not work
- Root required - System directory access needed
Breaking Changes
⚠️ Upgrading from v0.2? Read this before updating to v0.3+
v0.2 → v0.3 Migration Guide
1. Automatic Wrapper Syncing
The py2droid-update-bin command has been removed.
Wrappers now sync automatically at boot.
What to do:
- Remove any
py2droid-update-bincalls from your scripts - Use
. /data/adb/py2droid/env.shat the start of your scripts instead - Or just reboot to sync wrappers
2. Python Prefix Cleanup on Updates
The Python installation directory (/data/adb/py2droid/usr) is now cleaned during updates.
This removes globally-installed pip packages.
What happens:
- Packages installed without
--userflag will be deleted - The installer creates a backup:
/sdcard/py2droid-packages.txt
What to do:
- After updating, check
py2droid-packages.txtfor your packages - Reinstall:
pip3 install --user -r /sdcard/py2droid-packages.txt - Always use
--userflag:pip3 install --user <package>
Usage
Basic Commands
# Check Python version
python3 --version
# Run a script
python3 script.py
# Install packages (always use --user!)
pip3 install --user requests beautifulsoup4
Installing pip
Pip is included since v0.3.0. If you need to reinstall it:
python3 -m ensurepip --user
# Reboot or source the environment
pip3 --version
Virtual Environments
# Create venv
python3 -m venv venv
# Activate (must load env.sh first!)
. /data/adb/py2droid/env.sh
. venv/bin/activate
# Install packages
pip3 install -r requirements.txt
# Run your app
python3 main.py
In Magisk Modules
customize.sh, service.sh, action.sh
Wrappers are available:
# customize.sh example
cd "${MODPATH}" || exit 1
python3 main.py
post-fs-data.sh, uninstall.sh
Wrappers aren’t mounted yet. Load the environment manually:
# post-fs-data.sh example
. /data/adb/py2droid/env.sh # Load Py2Droid environment
cd "${0%/*}" || exit 1
python3 main.py
Customization
Create /data/adb/py2droid/.shrc to customize the environment:
# Example .shrc
export NAME="Mrakorez"
hello() { echo "Hello, ${NAME}!"; }
This file is sourced when loading env.sh (not during module installation).
How It Works
Directory Structure
Py2Droid uses /data/adb/py2droid as its home directory:
/data/adb/py2droid/
├── .cache/ # Cache directory (XDG_CACHE_HOME)
├── .config/ # Config directory (XDG_CONFIG_HOME)
├── .local/
│ ├── bin/ # User binaries (XDG_BIN_HOME)
│ ├── share/ # Shared data (XDG_DATA_HOME)
│ └── state/ # State files (XDG_STATE_HOME)
├── .tmp/ # Temporary files (TMPDIR)
├── env.sh # Environment loader
└── usr/ # Python installation (PREFIX)
├── bin/ # python3, pip3, etc.
└── lib/ # Python libraries
Command Wrappers
Commands like python3 in /system/bin are wrapper scripts:
#!/system/bin/sh
. "/data/adb/py2droid/env.sh" && exec python3 "$@"
These wrappers:
- Load the Py2Droid environment
- Execute the actual command
- Are created automatically at boot for new commands
- Are removed if the underlying command is uninstalled
Two Ways to Use Python
1. Wrappers (Quick & Isolated)
Use Python commands directly from any shell:
python3 -c "print('Hello')"
pip3 install --user requests
Pros:
- Works from anywhere
- Doesn’t affect your current environment
- Simple to use
Cons:
- New commands require reboot to become available
2. Direct Environment (Full Access)
Source the environment for immediate access to all changes:
. /data/adb/py2droid/env.sh
# Now everything is available immediately
pip3 install --user pipx
pipx install cowsay
cowsay -t "No reboot needed!"
Pros:
- Immediate access to newly installed commands
- Full environment control
Cons:
- Overrides your current environment (including HOME, PATH, etc.)
- Only works in POSIX-compatible shells (sh, bash, zsh, ash)
Building from Source
Prerequisites
Follow CPython Android build prerequisites
Build Steps
# Clone the repo
git clone https://github.com/Mrakorez/py2droid.git
cd py2droid
# Set up Python environment
python -m venv .venv
. .venv/bin/activate
pip install -r scripts/requirements.txt
# Build (see build.toml for configuration)
python scripts/build.py
# Output will be in dist/
CI/CD
Releases are automated via GitHub Actions:
- Make changes and commit
- Use
release.pyto prepare release - Push commits and tags
- GitHub Actions builds and publishes automatically
License
MIT License - See LICENSE for details.
探索相关项目
加载中…