How to Install and Use Python in Termux for Beginners

Learn how to install Python in Termux on Android. Perfect guide for beginners wanting to code scripts on their phone.

Python is one of the most popular and easiest programming languages to learn today. If you want to practice Python on your Android device, Termux is the right place to start. Termux gives you a Linux environment inside your phone. With it, you can install Python and run scripts anywhere, anytime.

Install Python in Termux on Android showing a smartphone terminal with Python logo and code, demonstrating a full mobile coding setup without root

In this guide, I will show you how to install Python in Termux and share some basic projects you can try.

Why Use Python in Termux?

Many people think they need a computer to start programming. But you can start right from your Android phone. Termux makes that possible. Here are some reasons why using Python in Termux is useful:

  • You only need a phone. No computer required.
  • It works even on low storage or budget devices.
  • You can practice coding anywhere, without internet.
  • It’s good for small scripts and automation tasks.

Python inside Termux is powerful. You can still install packages, run scripts, and test small projects. This is enough for learning and practicing.

How to Install Python in Termux

Before we start, it is important to know where to download Termux.

Do not download Termux from the Google Play Store. That version is outdated and will give you errors. Always use the latest version from F-Droid or GitHub.

Once you have Termux installed and ready, follow the steps below to install Python in Termux.

Step 1: Update and Upgrade Termux

First, update Termux to avoid errors during installation. Open Termux and type:

pkg update && pkg upgrade -y

When you run this command for the first time, Termux will download many files. During this process, you will see question like: ... (Y/I/N/O/D/Z) [default=N] ?

This is just Termux asking if you want to keep your current settings or install a new version of a configuration file. Just press Enter for every prompt. This selects the "default" option. It may ask you 4 or 5 times just keep pressing Enter until you see the $ prompt again!

Step 2: Install Python

Installing Python in Termux is very simple and straightforward. Just run the following command:

pkg install python -y

Wait for the installation to finish. This command installs the latest version of Python along with pip, which is the tool you will use later to install extra libraries. To confirm that it worked, check the version:

python --version
Checking Python version

If you see a version number, then Python is ready. For example, it might show something like Python 3.12.11.

Open Python Shell

After installation, you can run Python directly inside Termux. Just type:

python

You will see the Python interpreter which look like this:

>>>
Opening Python Shell

This is the Python shell. Here you can write and test Python code line by line without creating a file. For example, try this:

print("Hello Termux")

You should see the message "Hello Termux" printed on your screen. This is the simplest way to run Python commands.

When you finish and want to go back to the regular Termux terminal, just type exit() and press Enter, or hold the Ctrl key and press D.

Write and Run Python Scripts

Writing code directly in the shell is fine for quick testing. But if you want to save your work so you can use it later, you should create a Python file (also called a script).

To create a file, we use a simple text editor called nano followed by a file name. For example, type:

nano hello.py

This opens a text editor named hello.py (you can change the name if want, just make sure it ends in .py). Inside this editor, you can type any Python code.

For example, type:

print("Hello, Termux User!")
Writing code in Python shell

Save the file by pressing CTRL + X, then Y, then press Enter. Now, you can run the script by tryping:

python hello.py
Printing Python text

Your program should display the text you wrote. This is how you create and run Python scripts in Termux.

Tip!If you don't see the row of keys (like Ctrl, Alt, and arrows) above your keyboard, hold Volume Up and press Q on your keyboard to show or hide it.
I highly recommend installing Hacker’s Keyboard from F-Droid. It gives you a full QWERTY layout with all the essential keys like Ctrl, Alt, Esc, and the arrow keys, making your phone feel like a mini-laptop and makes editing code in Nano much faster!.

Installing Python Packages

Python is powerful because of its packages (pre-written codes that allow you to do things like web scraping, data analysis, or automation without starting from scratch).

We use pip To manage these packages. As we mentioned in Step 2, pip is installed automatically when you install Python in Termux. To install a new library, such as the popular requests library (used for interacting with websites), simply run:

pip install requests

Once the installation is complete, you can use the library in your Python scripts. Here is a quick example of how to use it:

import requests

response = requests.get("https://example.com")
print(response.status_code)

This simple script fetches a website and checks if it is online then prints the status code (like 200 for success). This is just a small taste of the automation you can achieve with Termux and Python!

Keep Pip Updated:
It’s a good idea to update pip occasionally to ensure it works smoothly with the latest packages. You can do this by running: pip install --upgrade pip

Manage Your Files

    As you write more scripts, it’s better to keep them organized. You can create a folder in Termux using mkdir command:

    mkdir python_projects
    cd python_projects

    If you want to access files from your phone’s internal storage (like your Downloads folder), you need to give Termux permission first:

    termux-setup-storage

    This will create a storage folder inside Termux. You can find it in /data/data/com.termux/files/home/storage. It links to your phone’s downloads, documents, and pictures. For example:

    • ~/storage/downloads → Phone downloads folder
    • ~/storage/shared → Internal shared storage
    • ~/storage/pictures → Phone pictures

    This makes it easy to move Python files between Termux and your regular phone storage for backup or sharing.

    Fix Common Problems

    Sometimes you may face errors while setting up. Don’t worry! Here are the most common problems and how to fix them quickly:

    Problem Fix
    Python command not found Re-run pkg install python -y.
    Pip not working Try: pkg install python-pip or pip install --upgrade pip.
    Permission error when saving files Make sure you are inside your home folder: cd ~.
    Package install fails Update Termux: pkg update && pkg upgrade -y.

    Beginner-Friendly Python Projects in Termux

    After learning the basics, the best way to improve is by building things. Here are a few small projects you can try right inside Termux:

    • Calculator: Write a script that takes two numbers and performs addition, subtraction, multiplication, or division.
    • To-do list: Create a simple text-based app where you can add, view, and remove tasks saved in a .txt file.
    • File manager: Write a script that lists all the files in your Termux home directory and tells you their size.
    • Weather checker: Use the requests library you just learned about to fetch weather details for your city from a free API.
    • Password generator: Write a script that uses the random and string modules to create strong, secure passwords for your accounts.

    These projects will help you get comfortable with Python fundamentals like variables, loops, functions, and error handling.

    Pro Tip: Try starting with the Password Generator! It’s only a few lines of code but it’s very useful. Once you finish a project, try adding a new feature to it to challenge yourself.

    You can also check out quick Termux projects to keep your momentum going.

    Use Python for Learning Cybersecurity

    Many people use Termux to learn about cybersecurity. Python is one of the most important languages in this field.

    You can use it to write simple scripts that test password strength or check if a website is secure. It's a great way to learn how the web works while practicing your coding skills. I will share more posts about safe cybersecurity projects very soon!

    Installing Python in Termux is simple, but it opens up a world of possibilities. One command gives you a full coding setup in your pocket! The real progress comes from daily practice. Start small, stay organized, and try to build scripts that solve your own daily problems.

    About the author

    Stephano kambeta
    Proudly African 🇲🇼. Modern life, shaped by the echoes of traditional drums.

    Post a Comment