How to Install Python in Termux
How to Install Python in Termux: A Complete Step-by-Step Guide
Python remains one of the most versatile, beginner-friendly, and powerful programming languages in the world today. Whether you are interested in web development, automation scripts, ethical hacking, data analysis, or building Telegram bots, Python is often the language of choice. Thanks to modern Android capabilities and terminal emulators, you do not need a bulky desktop computer to start writing and running Python code. You can carry a full-fledged development environment right in your pocket.
Termux is a powerful terminal emulator and Linux environment application for Android that works directly without requiring root access. A minimal base system is installed automatically, and additional packages are available using the APT package manager. If you want to configure a mobile coding environment, learning how to install python termux setups properly is your very first step.
In this ultimate guide, you will learn how to install python 3.12 termux packages, verify your installation, set up essential development dependencies, run your first script, and troubleshoot standard setup errors seamlessly.
Prerequisites Before Installing Python
Before executing installation commands, ensure your Termux environment is prepared correctly. Following these baseline requirements guarantees that package downloads do not fail halfway through.
- Download Termux from F-Droid: Do not install Termux from the Google Play Store. The Play Store version is deprecated, unmaintained, and fails to update repositories correctly. Download the latest APK from F-Droid or GitHub.
- Active Internet Connection: Python and its base dependencies require roughly 100MB to 250MB of data download during initial setup.
- Sufficient Device Storage: Ensure your phone has at least 500MB of free internal storage to store Python libraries and virtual environments.
Step 1: Update and Upgrade Termux Packages
Always update your core package index and upgrade existing software before adding new programs. This prevents library version mismatches and broken package locks during installation.
Open Termux and enter the following combined command:
pkg update && pkg upgrade -y
If prompted during the update process regarding configuration files, press Enter to accept the default options. Maintaining an updated system ensures you download the newest stable release built for your smartphone's architecture (ARM64, armv7, or x86_64).
Step 2: Command to Install Python in Termux
Termux simplifies software installations through its optimized package manager (pkg). To install python termux core files, type the following command and hit enter:
pkg install python -y
This command automatically fetches the main Python interpreter alongside pip (Python's official package installer), setuptools, and essential dependency libraries required to run Python scripts smoothly.
If you specifically need legacy Python 2 for older scripts or specialized security tools, you can install it separately alongside modern Python using:
pkg install python2 -y
Step 3: Verify the Python Installation and Check Version
Once the download completes, you should verify that the binary was added to your environment path. Checking the installed version confirms whether you successfully managed to install python 3.12 termux packages on your phone.
Run the version check command:
python --version
Expected Terminal Output
Your terminal screen will display an output similar to the screenshot representation below, displaying the current release branch:
$ python --version
Python 3.12.2
$ python
Python 3.12.2 (main, Feb 20 2024, 11:10:00) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, Termux!")
Hello, Termux!
>>> exit()
$
If you see Python 3.12.x printed in your terminal, your interpreter is properly configured and ready to execute scripts!
How to Write and Run Your First Python Script in Termux
Now that Python is installed, let's create and execute a simple Python program using the terminal editor.
1. Create a Project Directory
Keep your project files organized by creating a dedicated folder:
mkdir my_python_projects
cd my_python_projects
2. Write a Simple Script
You can use terminal text editors like nano or micro to write code. Install nano if you haven't already:
pkg install nano -y
nano main.py
Inside the nano text editor, type the following lines of code:
import sys
print("Welcome to TermuxGenius!")
print(f"Running on Python version: {sys.version}")
Press CTRL + O then Enter to save the file, and press CTRL + X to exit the editor back to the shell.
3. Execute the Script
To run your script through the Python interpreter, enter:
python main.py
The code will execute immediately and print the output right inside your Android terminal window.
Essential Build Tools for Installing Pip Packages
Installing Python itself is only half the journey. Many popular Python libraries (such as numpy, cryptography, pillow, or pandas) rely on underlying C/C++ extensions that must be compiled during installation via pip.
To prevent compilation errors when downloading modules, install essential build tools and C compilers in Termux using this single command:
pkg install build-essential clang python-dev libffi libxml2 libxslt -y
After installing these dependencies, you can install any Python package smoothly using pip:
pip install --upgrade pip
pip install requests colorama
Common Termux Python Errors and How to Fix Them
Even when following instructions closely, mobile environments can throw unexpected errors due to repository changes or permission issues. Here are the most frequent errors and their exact solutions.
Error 1: Unable to locate package python
Cause: Your local repository package list is outdated or points to an inactive mirror server.
Fix: Switch your Termux repository mirror using the built-in repository utility and update again:
termux-change-repo
Select 'Single mirror', choose an active official mirror (such as Grimler or Al権s), click OK, and then run:
pkg update && pkg install python -y
Error 2: Permission Denied when Reading/Writing Files
Cause: Termux does not have access to your Android internal storage directories (such as Downloads or Documents).
Fix: Grant storage permissions to Termux by running:
termux-setup-storage
A popup prompt will appear on your phone screen. Tap Allow. You can now read and write files directly inside the ~/storage/shared path.
Error 3: Failed building wheel for cryptography / C extension modules
Cause: Missing standard header libraries, rust compiler, or C dependencies necessary for compilation.
Fix: Export standard library flags and install Rust along with OpenSSL headers before invoking pip:
pkg install rust binutils openssl-dev matrix-ssl -y
pip install --upgrade setuptools wheel
pip install cryptography
Summary Checklist
To recap, setting up Python on Android requires three main command stages:
pkg update && pkg upgrade -y(Update repositories)pkg install python -y(Install Python 3.12 stack)pkg install build-essential clang -y(Prepare environment for pip wheels)
What to Do Next?
Congratulations! You have successfully configured a clean Python environment on your Android device. Now you are ready to venture into advanced automation and development projects.
Check out our step-by-step follow-up tutorial on How to Install and Manage Pip Packages in Termux to learn how to fix complex wheel compilation issues, build virtual environments (venv), and manage project dependencies like a pro!
Join the conversation