I believe the solution for you is Python-for-Android (Py4A). Not only does it allow you to run scripts on your Android device, but you can go one step further because the project includes simple draft instructions on creating a standalone, downloadable Android APK package.
First, I would start by going to the readme section of Py4A. There is a recipes (script) that will fit your needs. The python3 recipe allows Python3 to run as a "standalone" on Android.
This excerpt from Linux Journal explains script installation on your Android device:
Running on a Smartphone
To run a Python script on your physical Android device, install SL4A together with Python for Android on your handset, then transfer your script.
To install SL4A on your physical Android device, enable the Unknown Sources option in your device's Application settings. This setting is required to enable the installation of non-Market apps on your phone. With this done, you can follow the same steps you used when installing SL4A and Python on your emulator. To speed things up a little, install Barcode Scanner from the Android Market and use it to “read” the QR Codes from your desktop screen.
Transferring Your Script to Your Handset
There are a number of ways to get your script onto a real phone. I've found the success of using something like Bluetooth connectivity or USB cabling arrangements can very much depend on the hardware on which you're running. What works on one handset, doesn't on another, and so on. Your mileage may vary depending on your actual device. When I need to transfer a file, I've come to rely on a solution that works no matter which handset I use (as long as the handset can talk to a local Wi-Fi network). What I do is switch on the OpenSSH server on my development PC running Linux, then use the AndFTP file transfer app on the handset to scp files from the desktop to the phone. AndFTP is available from the Android Market as a free download and installs in minutes. Once I connect to my desktop with AndFTP, I can navigate to a directory of my choice, mark the files that I want, then download them to my SD card on the handset.
AndFTP works well, and I've come to depend on it for all my Android file transfers (see Resources). Just be sure to transfer your scripts to /sdcard/sl4a/scripts on the phone to ensure that your script names appear within the SL4A list of scripts.
With your script file transferred to your physical device, start SL4A as before, tap your app's name and tap the run wheel. As expected, your app runs just as it did on the emulator, only faster! I haven't included a screenshot of the app running on a real phone for two reasons. First, it looks exactly the same as it did in the emulator, and second, it's running on your device, so you can take a look at it there!
Creating an APK
There's one further kink to SL4A that might interest you. The project includes draft instructions on creating a standalone, downloadable Android APK package (see Resources). Once created, the APK file bundles your custom Python script with information that allows other Android users to install Python for Android automatically onto their handsets and then run your app from the smartphone's main menu of apps. Describing the process of creating the APK likely would take another article, so I leave it to the brave among you to try out the instructions on the SL4A Wiki"
Python-for-Android website.
Answer from Bo Lawson on Stack ExchangeI am wondering if it is possible to run a python script from a mobile phone. Is there an app that simulates something like a bash terminal window? And since my phone is almost always up and running, would it be possible to use it as a remote server (think DigitalOcean's droplet) with scheduled jobs (cron)? Ideally, I could create (edit, test) my scripts on my Linux machine and then run them from my mobile.
Executing python script in android terminal emulator - Stack Overflow
Python Interpreter on Android - Stack Overflow
terminal - How to run Python 2 and Python 3 CLI programs on Android 14 phones? - Android Enthusiasts Stack Exchange
How can I run a Python script in terminal emulator? - Android Enthusiasts Stack Exchange
Videos
You can use the termux app, link to homepage, which is available in the google market and requires no root. It is a terminal emulator with a full Linux environment and via apt install python you can install python. apt list gives you a list of all available packages.
There's also Python for Android (Py4A), which is intended to work with SL4A. The documentation for Py4A is not as extensive as SL4A, but there are a few examples for Py4A available here.
In addition, there's Kivy, a crossplatform Python-based development framework for Android, PC, Linux, and iOS. I've played around with Kivy a little bit in the past, and while it's very powerful, it's documentation has been lacking, and the code is difficult to decipher.
You could use UserLAnd, GNURoot Debian, or Termux. Termux is probably the most lightweight, and also has Swipe keyboard support (have to swipe the entire input to the side to access it), and you can enable an extended keyboard that pops up over whatever keyboard you use to access arrows, Tab, Ctrl, etc. UserLAnd also has the extended keyboard and the same Swipe support, and runs a full version of Linux in a chroot environment so you can have root access using sudo or the root user.
Using Termux & UserLAnd on an unrootable Android and both are amazing. rsync is the greatest thing ever to keep project changes up to date because AFAIK it's not possible to edit the data stored in either of their filesystems directly using an Android app, but can be done using vi/vim/nano/etc inside of Termux or in UserLAnd's session (have to connect to it through SSH or VNC).
Just make sure to follow the suggestions during UserLAnd setup and grab ConnectBot and/or bVNC -- those are currently the only supported apps to connect to it. Also, while it claims to be able to setup for a single app I have not seen any directions for that, and have no idea how it would be possible as the UI doesn't seem to have anyway to do so.
Your file permissions are not appropriate. Change it using chmod +x path_to_file and then run it using file_name.py.
Hi,
Is there a mobile app that allows you to code python and display the output in a console?
I'm on the road for 2 months and am working from a textbook and can't necessarily always use my laptop to plug in practice exercises.
If not an app, potentially a website like CodePen that anyone can recommend? However, an app would be preferential.
Cheers.
Edit: should have mentioned I'm using an android device.
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
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.
First: As I have seen some a bit hostile responses to questions like this here so please tell me to fuck off if I'm in wrong. :)
I want to be able to run Python programs on my Android phone. The main usage will be file management and editing of text files etc. Maybe use it to control or monitor activities on the phone. I'm not asking for anything about coding in Python, maybe except Android specific things. (I'm not familiar with coding with Python but will learn that by other means).
So in short: what to install, how to install (in short - I have not any experience in installing these kind of software on my phone) and elementary pitfalls.
(Background: have been programming professionally since 1979 and are still doing that. Mainly mainframe.)
Thomas