How to Install and Use Python in Termux

Python is a most popular and powerful easy-to-learn programming language that is used for many tasks like web development, automation, and data analysis.

If you are an Android phone user and want to use Python on your Android device, then you can install and use Python with the help of Termux application.

Termux is a terminal application where you can install and use Linux tools and packages. You can also install and use multiple Python libraries and run Python scripts on your Android device using Termux.

In this post, we will explain everything about Python in Termux from installing Python in Termux to using it.

Python in Termux
Python in Termux

Why Use Python in Termux?

There are many features of using Python in Termux.

  • Portability: You can easily carry your smartphone everywhere with python setup.
  • Python libraries: You can use almost all python libraries.
  • Easy Setup: Install Python and its libraries with some simple commands.
  • Development on the Go: perfect for developers and beginners who want to code anywhere anytime.

These are the advantages of using Python in Termux.

How to install Python in Termux

You can easily install Python in Termux but before that you need to update and upgrade installed Termux packages.

Commands to update and upgrade Termux packages:

apt update && apt upgrade -y

Now install Python by typing this single command:

pkg install python -y

After running these two commands Python will be successfully installed in your Termux. Check its version by typing:

python --version
Check Python Version
Check Python Version

This will display the installed Python version.

Once Python is installed in Termux, it usually includes pip, the Python package installer. Pip installation is required to install python libraries in Termux. You can check its version for verification.

Command to check Pip version in Termux.

pip --version

If pip is installed, this command will display its version, like here you see the screenshot:

Check Pip Version
Check Pip Version

How to install Python 2 in Termux

As we know, Python 2 is officially ended and no longer maintained, but some tools and projects might still require Python 2, so here are the command to install Python 2 in Termux.

pkg install python2

This command will start the python2 installation process.

After installation, You can verify it by checking its version.

Command to check Python2 version.

python2 --version

it will show you the install python2 version.

Check Python2 Version
Check Python2 Version

Python 2 also comes with the default pip2 package installer for Python 2.

Command to check Pip2 version.

pip2 --version  

This will show you the Pip version for Python2, such as:

Check Pip2 version
Check Pip2 version

How to install Python 3 in Termux

Python 3 is the latest version of modern Python programming, and Python 3 will install by default when you install Python, but if it does not install, you can run the following command:

Command to install Python3 in Termux.

pkg install python3

This command will start the python3 installation process.

For verification, you can check its version.

Command to check Python3 version.

python3 --version  
Check Python3 Version
Check Python3 Version

You can also check the Pip version for Python 3, which is called Pip3 (Package Installer for Python 3).

Command to check Pip3 version.

pip3 --version
Check Pip3 Version
Check Pip3 Version

We have successfully installed all available Python versions along with their package installers (Pip). Now, It’s time to use python in Termux.

How to use Python in Termux

Python in Termux is a flexible and portable way where you can write, run, and experiment with Python scripts. As we have already installed Python in Termux, below is a simple guide for new Python users who want to learn Python from their smartphone.

  • Creating a Python file:

Use touch command to create a python file. For example:

touch script.py

Will creates a empty python file ‘script.py’ in your Termux.

  • Open a Python file:

Use cat command to open python files in Termux. For example:

cat script.py
  • Write your Python code:

Termux comes with Nano text editor, a simple text editor where you can write your own Python codes. For example:

nano script.py

It will launch Nano text editor. Here you can see the screenshot:

Nano text editor
Nano text editor

Press (Ctrl+O, then press Enter button, and Ctrl+X) to save your python file and exit from the nano text editor.

  • Run a python file:

Use python command with file name to run python files in Termux. For example:

python script.py
Run python files
Run python files
  • Install Python libraries:

Use the pip command to install Python libraries. For example:

pip install numpy
  • Use the Installed Library:

After installation, you can import and use the library in your script, for example here is a numpy python code to add two numbers.

import numpy as np

# Take two numbers as input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Use numpy to add the numbers
result = np.add(num1, num2)

print(f"The sum of {num1} and {num2} is: {result}")

You can save this code by pressing (Ctrl+O, then press Enter button, and Ctrl+X) then you can run this code by python script.py command.

Practical Usage of Python in Termux

Python can be used to perform various tasks in Termux within the Android device. Here are some practical ways to unlock the full potential of Python in Termux:

  • Learning Python

Termux provides an excellent environment for learning Python programming, as it supports a variety of libraries and modules.

  • Task Automation

Python can be used to automate tasks like file management, downloading files or data transformation.

For example, here is a simple Python task automate code that you can use in Termux to organize files by their extensions in a directory.

import os

def organize_files():
    [os.mkdir(f"{os.getcwd()}/{ext}") for ext in {f.split('.')[-1] for f in os.listdir() if os.path.isfile(f)} if not os.path.exists(f"{os.getcwd()}/{ext}")]
    [os.rename(f, f"{os.getcwd()}/{f.split('.')[-1]}/{f}") for f in os.listdir() if os.path.isfile(f)]

if __name__ == "__main__":
    print("Organizing files by extension...")
    organize_files()
    print("Files organized successfully!")
    
  • Development Projects

You can build or test web applications with the help of frameworks like Flask or Django.

Command to install Flask.

pip install flask

A simple example code of a Python web application project using the Flask library.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello from Termux Flask App!"

app.run(host='0.0.0.0', port=5000)
  • Ethical Hacking and Networking

Python has libraries like scapy or socket, which you can use to create network scanner tools, packet sniffers or even test web vulnerabilities.

Command to install scapy in Termux

pip install scapy

Tips for Using Python in Termux

Here are some tips to use Python efficiently in Termux.

  • Explore Termux API:

You can use termux-api to integrate Android features into your Python scripts.

  • Backup Your Work:

Regularly back up your scripts to cloud storage like Google drive or Git repositories.

  • Keep Python Updated:

Update python and pip versions on a regular basis.

Commands to update Python and Pip.

pip install --upgrade pip

pkg upgrade python
  • Explore Advanced Libraries:

You can explore and test advanced Python libraries like Numpy, Pandas, and Matplotlib to build advanced projects.

By using these tips you can maximize the potential of Python in Termux.

Conclusion

Python in Termux gives you a platform where you can write, run, and manage projects directly on your Android device. You can learn Python programming, develop advanced programs, and do automation tasks within your smartphone.

In this post we have provided simple commands to install Python in Termux and explained how to use it. With the help of this blog post, you can learn Python, write your own Python code, and you can also use advanced Python libraries.

SHARE THIS POST:

Leave a Reply

Your email address will not be published. Required fields are marked *