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 there from the library of PlayStore.

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 which comes with the 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 not needed 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 it is 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 totally possible, here is how:

1) Download and Install Pydroid 3 IDE for Android

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

PyDroid App Download

  1. Once the process of installation gets 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 simply 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 which shall offer you with most popular Linux commands along with many hundreds of additional packages for an easy process of installation. The best thing is that it does not need any kind of special permissions, you can either use default Google Play store or also 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’s pkg command which is as followed:

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

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

[email protected]:~# install root-repo

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

[email protected]:~# update

Next, install Python by using the following command:

[email protected]:~# 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 now you can work on Android Smart Phone devices largely as if it was yet another Linux system. This however is a great demonstration of how powerful the terminal really is.

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

[email protected]:~# mkdir ninja-android-project
[email protected]:~# cd ninja-android-project

After this, you need to create Python virtual environment. This is quite a common practice for developers and it 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:

[email protected]:~# python –m venv venv

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

[email protected]:~#./venv/bin/activate

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

Now you need to install Flask Python module:

[email protected]:~# install flask

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

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

As the demonstration application is simple for now you can just 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 which is 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 that is running there in the local host which is your device. This is a server that lists requests looking for port 7000.

There 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 which means that the phone is not accepting any kind of requests from the world outside. Only you have access to your Flask server.

If you intend to make your server visible then you can disable flasks debugging mode by adding in –host=0.0.0.0 to run the command. It is for sure open ports so make sure to use it wisely.

To stop the server you simply need to press Ctrl + C there from the keyboard.

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

Conclusion

Android itself is a very popular platform and 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here