19. Installing Python and Running Scripts
Python is one of the most popular languages for scripting, web development, and data analysis. Termux provides excellent support for it.
1. Installation
Python (specifically Python 3) is installed using pkg.
bash $ pkg install python
Verify the installation:
bash $ python --version Python 3.X.X
2. Using the Python Interpreter
You can launch the interactive Python shell by simply typing python:
bash $ python Python 3.X.X (default, Jun 1 2024, 10:00:00) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
print('Hello from Python') Hello from Python exit() $
3. Running a Python Script
Create a small Python file (using nano):
bash $ nano greetings.py
Content of greetings.py:
python #!/usr/bin/env python
name = input("Enter your name: ") print(f"Hello, {name}! This ran successfully in Termux.")
Execution:
bash $ python greetings.py Enter your name: Alex Hello, Alex! This ran successfully in Termux.
4. Installing Libraries (pip)
Python's package installer, pip, is installed automatically with Termux's Python package. Use it to install external libraries like requests or numpy.
bash $ pip install requests