This Magisk module contains Python3 3.14 for Android

Also included in the Magisk Module are the OpenSSL libraries necessary to use SSL in Python scripts (in /system/usr/lib). 
The Python module to install other modules, pip, is already installed.


There are two Python executables in the module, the binary python3.14 needs the Python library in /system/usr/lib and the binary python3.14-static only needs the standard Android OS libraries:

ASUS_I006D:/ $ LD_LIBRARY_PATH=/system/usr/lib ldd /system/usr/bin/python3.14
        linux-vdso.so.1 => [vdso] (0x7a9b8df000)
        libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7a95112000)
        libpython3.14.so.1.0 => /system/usr/lib/libpython3.14.so.1.0 (0x7a96695000)
        libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7a9a69d000)
        liblog.so => /system/lib64/liblog.so (0x7a9a647000)
        libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7a96fc1000)
        libc++.so => /system/lib64/libc++.so (0x7a95048000)
ASUS_I006D:/ $


ASUS_I006D:/ $ ldd /system/usr/bin/python3.14-static
	linux-vdso.so.1 => [vdso] (0x7c7e2c0000)
	libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7c7756e000)
	libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7c7d046000)
	liblog.so => /system/lib64/liblog.so (0x7c7752c000)
	libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7c7d086000)
	libc++.so => /system/lib64/libc++.so (0x7c79e07000)
ASUS_I006D:/ $ 

(A "real" static binary for Python is not really usefull because static binaries in Android can not resolve hostnames via DNS)


The standard executable is the binary that requires the Python library; therefor the wrapper script 

/system/bin/python_wrapper 

is used to execute the python executables:

ASUS_I006D:/ $ ls -l /system/bin | grep python_wrapper                                                                                                                                                           
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 idle3 -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 pip -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 pip3 -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 pip3.14 -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 pydoc3 -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 python3 -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 python3-config -> ./python_wrapper
lrwxrwxrwx 1 root root       16 1970-01-08 21:03 python3.14-static -> ./python_wrapper
-rwxr-xr-x 1 root root     1241 2025-04-01 14:36 python_wrapper
ASUS_I006D:/ $ 

Note that the wrapper script also sets other environment variables for Python (see below).


The configured directories for site-packages are:

ASUS_I006D:/ # python3 -m site
sys.path = [
    '/',
    '/system/usr/lib/python314.zip',
    '/system/usr/lib/python3.14',
    '/system/usr/lib/python3.14/lib-dynload',
    '/system/usr/lib/python3.14/site-packages',
    '/data/local/tmp/home/python3.14/site-packages',
    '/data/local/tmp/home/python/site-packages',
]
USER_BASE: '/data/local/tmp/home/.local' (exists)
USER_SITE: '/data/local/tmp/home/.local/lib/python3.14/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
ASUS_I006D:/ # 


The directories 

/data/local/tmp/home/python3.14/site-packages 
/data/local/tmp/home/python/site-packages
/data/local/tmp/home/.local/lib/python3.14/site-packages

are writable by the user shell and can be used to add additional site-packages.


pip can be used to install additional Python modules; the target directory for these modules defined in the wrapper script is

/data/local/tmp/home/python/site-packages

To use another target directory for the Python modules export the variable PIP_TARGET before executing pip.
Or use the parameter "--target=<dir>" for pip to set the target directory.


The wrapper script defines the environment variable SSL_CERT_DIR with the value /system/etc/security/cacerts if it is not already defined.

Use 

python -c "import ssl; print(ssl.get_default_verify_paths()) "  

to show which certificate paths are being used.



The wrapper script defines the environment variable PYTHONPYCACHEPREFIX with 

/data/local/tmp/home/python/cache

This directory is then used for the compiled Python scripts. The customization script creates this directory if it does not yet exist.


The wrapper script defines the variable TERMINFO with 

/system/etc/terminfo 

or 

/system_ext/etc/terminfo

if one of these directories exists and the variable TERMINFO is not already defined


The wrapper scripts sets the environment variable LD_LIBRARY_PATH to 

/system/usr/lib


If the directory 

/data/local/tmp/home/libs 

exists it is also added to the environment variable LD_LIBRARY_PATH. This directory should be used for addition library files required by the Python scripts.


Set the variable DEBUG to any non-empty string before executing python to execute the wrapper script with "set -x", e.g.:

DEBUG=yes python --version           


If the file

/data/local/tmp/home/python_init.sh

exists, it will be sourced in by the wrapper script before executing the Python binary.

The file /data/local/tmp/home/python_init.sh can be used to define or overwrite the environment variables for Python ( ... or do anything else)

(see https://docs.python.org/3/using/cmdline.html for a list of supported environment variables; use the variable "$0" in the script to distinguish between the executables)


The Python executables from this Magisk Module were compiled on an ASUS Zenfone 8 with the clang19 toolchain for android 

see 

  http://bnsmb.de/My_HowTos_for_Android.html#How_to_install_a_Toolchain_for_clang_on_phones_without_root_access

or 

  https://xdaforums.com/t/guide-how-to-install-a-toolchain-for-clang-on-phones-without-root-access.4710235/


The clang19 toolchain is also available on github: 

  https://github.com/bnsmb/clang19_toolchain_for_android/


All approbiate patches from the Termux repository for Python

  https://github.com/termux/termux-packages/tree/master/packages/python

as of 06.10.2024 are applied.

The patches applied are in the directory source/patches in the Magisk Module.


 ------------------------------------------------------------------------------

The config used to compile Python is:

[ OmniRom 15 - xtrnaw7@t15g /data/develop/android/ModuleSrc/python314/source ] $ cat myconfigure
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp/develop/sysroot/usr/lib

export  PKG_CONFIG=/data/local/tmp/sysroot/usr/clang19/bin/pkgconf

export CFLAGS="$CFLAGS -D__ANDROID_API__=31 -O3  -Wno-implicit-function-declaration -Wno-int-conversion "
export CPPFLAGS="$CFLAGS"

export TARGET_ROOT=/data/local/tmp/sysroot/

AC_OPTIONS="ac_cv_func_shm_unlink=no  ac_cv_func_shm_open=yesac_cv_posix_semaphores_enabled=yes ac_cv_func_linkat=no ac_cv_func_faccessat=no  ac_cv_file__dev_ptc=no"

LDFLAGS="$LDFLAGS /data/local/tmp/develop/sysroot/usr/lib/libncursesw.a"

# --enable-optimizations
eval ${AC_OPTIONS} ./configure  --prefix=${TARGET_ROOT}/usr --with-openssl=/data/local/tmp/develop/sysroot/usr \
  --enable-loadable-sqlite-extensions --enable-shared  --enable-pystats  --with-builtin-hashlib-hashes=md5,sha1,sha2,sha3,blake2,sha256,sha512 \
   --with-system-expat --without-ensurepip  --with-tzpath=/system/usr/share/zoneinfo --with-system_libmpdec

echo "Correcting the Makefile ..."

sed -i -e "s/none required//g" Makefile


The result is:

The necessary bits to build these optional modules were not found:
_posixshmem               _tkinter                                         
To find the necessary bits, look in configure.ac and config.log.

Following modules built successfully but were removed because they could not be imported:
_dbm                                                                       

Checked 112 modules (34 built-in, 74 shared, 1 n/a on linux-aarch64, 0 disabled, 2 missing, 1 failed on import)
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/done_clang/cpython314 $      



 ------------------------------------------------------------------------------


History

06.10.2024 v1.0.0
  initial release

08.04.2025 v2.0.0
  added pip 25.0.1
  recompiled the files with clang19 and the prefix /system
  added an additional Python executable that only needs the standard Android OS libraries: python3.14-static
  some Python scripts in the previous version of the module used a shebang with /data/local/tmp/python3/usr/bin/python3 instead of /system/bin/python3  -- fixed
  added the libraries necessary for using SSL in Python programs ; the libraries are in the directory /system/usr/lib to not overwrite the SSL libraries from the OS
  the wrapper script now defines the environment variable SSL_CERT_DIR with the value /system/etc/security/cacerts if it is not already defined
  the wrapper script now defines the environment variable PIP_TARGET with the value /data/local/tmp/home/python/site-packages if it is not already defined
  the wrapper script now defines the environment variable TERMINFO if it is not already set with either /system/etc/terminfo or /system_ext/etc/terminfo
    if one of the directories exist
  the wrapper script now adds the directory /data/local/tmp/home/libs to the environment variable LD_LIBRARY_PATH if it exists

