How To Install, Create and Run Python Programs on Android

Python is a versatile programming language that can be used to build sites, apps, automation, AI and software, automate tasks, and conduct data analysis. The platform is irrelevant as you can now Install, create and run Python programs on your Android smartphone. However, we would suggest the experience is somewhat addicting and very geeky!

It is quite a high-level, widely used, and general-purpose language that can run on Android via different applications from the PlayStore library.

Python provides the following in a nutshell:

  1. It offers full TKinter support for Graphical User Interface.
  2. It is a feature-rich Terminal Emulator that comes with a read-line backing.
  3. Examples are available out of the box for quick learning.
  4. Tensorflow is also available now.
  5. Offline Python version 3.7 interpreter: The Internet is unnecessary to run Python programs.
  6. Package manager and custom repository for pre-built wheel packages for enhanced scientific libraries like NumPy, scipy, matplotlib, Scikit-learn and Jupyter.

Install, Create and Run Python Apps/Programs on Android Smartphone

Just imagine building and running a Python app, whether a command line tool developed to fetch your favorite curated articles from the web or starting a web server that runs right in your hand. Well, it is possible, here is how:

1) Download and Install Pydroid 3 IDE for Android

  1. You need to install Pydroid Application, and for this head towards PlayStore.

PyDroid App Download

  1. Once the process of installation is completed, all you need to do is run the application and it shall show as installing Python.
  2. Wait for around a minute and it shall show the IDE, in here enter the Python code.
  3. After this hit the yellow button to run it.

Running Python app on Android

It has been installed successfully.

ALSO SEE: How To Create Your Own Bluetooth Signal Jammer App with Python (Source Code).

2) Termux Installation on Android

You need to install the Termux application, which is quite a powerful terminal emulator that offers you the most popular Linux commands along with hundreds of additional packages for an easy installation process. The best thing is that it does not need any special permissions, you can either use the default Google Play store or open the source application repository.

Once this has been installed, you need to launch it and perform certain requisite software installations with the help of using Termux pkg command which is as follows:

Just add “pkg” in the beginning of each command below before running it.

You need to subscribe to an additional repository “root-repo”:

Paul@ninja-ide:~# install root-repo

Then perform the update to bring all the installed software up to date:

Paul@ninja-ide:~# update

Next, install Python by using the following command:

Paul@ninja-ide:~# install python

Using Python code on Android with Termux

Once the process of installation and auto set-up of configuration is completed it’s now time to build the application.

Compile and Build Full Python Apps on Android

As the terminal has been installed, you can work on Android Smart Phone devices largely as if it were another Linux system. This however is a great demonstration of how powerful the terminal is.

First things first, you need to begin by creating a project directory:

Paul@ninja-ide:~# mkdir ninja-android-project
Paul@ninja-ide:~# cd ninja-android-project

After this, you need to create a Python virtual environment. This is quite a common practice for developers and also helps keep your project of Python independent of the development system.

Now, within the venv virtual environment you can install Python modules that are specific to the application, you should read our Python venv 101 guide before continuing to get a better understanding on how it works:

Paul@ninja-ide:~# python –m venv venv

You need to activate a new virtual environment then (keep in mind that two dots at the beginning are separated by space):

Paul@ninja-ide:~#./venv/bin/activate

Keep in mind that the shell prompt is now preceded by (env) to indicate that you are in a virtual environment

Now you need to install Flask Python module:

Paul@ninja-ide:~# install flask

Now you are all ready and only need to write code for the application.

You must have experience with a classic text editor such as Visual Studio. We have used vi. If you don’t have any familiarity with this then install and try vimtutor app which can teach you how to use it. If you somehow carry a different one you prefer like Emacs, you can install and use those as well.

As the demonstration application is simple for now you can use the shell’s heredoc function which allows you to enter text at your prompt directly.

This shall be a six-line code but with this, you need to import Flask, create an application and then route the incoming traffic towards the function known as hello_world.

After this you shall have the web-server code all ready, it is now time that you set up some environment variables and begin the web server on your smartphone device.

This informs that you carry a tiny web server running there in your local host, your device. This is a server that lists requests looking for port 7000.

On your smartphone, you need to open http://localhost:7000 to see the web application.

You have not compromised the security of your phone. You are just running a local server, meaning the phone is not accepting any requests from the outside world. Only you have access to your Flask server.

If you intend to make your server visible, you can disable flasks’ debugging mode by adding host=0.0.0.0 to run the command. It is an open port, so make sure to use it wisely.

Pressing Ctrl + C from the keyboard would be best to stop the server.

Remember: You can always change the port number, the default is 5000.

Conclusion

Android is a very popular platform that allows a lot of freedom and features on the device. You can create fully-fledged Python apps on Android using Pydroid and Termux with venv virtual environments. How much more geeking can you want? You are now successfully running Python code on Android.

Previous articleConfigure Raspberry Pi Remote Desktop To Windows 11/10, Mac & Linux
Next articleRaspberry Pi Sh File: Install, Create and Run Shell/Bash Script (With Examples)
Paul Carruthers
Paul is an avid programmer who specializes in Python and Java with over 16 years of experience in the field. He loves automating complex tasks and creating useful scripts to streamline work and make life easier. He is also a massive fan of Linux and currently uses it as his main desktop OS. When he is not staring at code, he loves hiking and swimming in different parts of the world.

2 COMMENTS

  1. Very interesting. I often wondered about running Python on Android. Rather than a web app, what are other ways you might put Python to work on Android? It would be interesting to see more examples.

    One question – you mention 6 lines of code but I don’t see any code. Specifically, you said “This shall be a six-line code but with this, you need to import Flask, create an application and then route the incoming traffic towards the function known as hello_world.”

LEAVE A REPLY

Please enter your comment!
Please enter your name here