Installing Python on Termux is very straightforward, but many users have encountered problems installing both python2 and python3 on Termux. In this article, we will teach you how to install and run Python on Termux for Android. Stay tuned with SPY24.
Installing Python on Termux
If you are interested in the Python programming language, the good news is that you can install Python on Termux and use it on Android.
- Python is a powerful language that allows you to create various tools and software.
- To run many programs, you need to have the Python interpreter installed.
- So, this tutorial is useful for you in any case.
- The first thing to do is to update Termux and its packages.
To do this, enter the following commands one by one in the Termux terminal:
pkg upgrade
pkg update
apt-get update
apt-get upgrade
apt update
apt upgrade
Tutorial on Updating Termux
The first relatively easy command you can execute is pkg update. After pkg, there should be a space and all letters should be written in lowercase. After typing the command, press Enter on the keyboard.
Now Termux is ready to install Python.
How to Install Python 2
To work with Python in Termux, you can install two different versions: python2 and python3. Depending on the course you have taken, you should execute the version you need according to the following instructions.
- Running the Termux app on your phone
- Type the following command
- apt update
- apt upgrade
- After the packages are updated, type the following command:
- apt-get python2
- After that, a 25-megabyte file will be downloaded upon selecting the option Y.
- After installing the 25-megabyte file, type the command python to run it.
How to install Python 3 on Termux
You have previously become familiar with the differences between Python versions 3 and 2 in Termux. Now, to install Python 3, you must follow the method below.
Open the application.
If you have not yet upgraded the installed packages, type and execute the following commands:
apt update
apt upgrade
Now that the packages are updated, you need to type and run the command apt-get python3. Press ‘Y‘ to install the Python package.
You can also use the following command to install python3:
pkg install python3
How to Use Python After Installing It in Termux?
To run programs written in Python 3, you should use the following command.
This command automatically executes programs written in Python 3 for you.
python example.py
Likewise, to use Python 2 and execute programs written in it, you can use the command below.
python2 example.py
This way, you can tell Termux to run the programs with Python 2 instead of the default version, which is Python 3.
The same goes for pip. If you want to use it, you should use the command below.
pip install example
This command automatically uses pip version 3 to install packages.
Also, to install packages with pip version 2, you can use the following command.
pip2 install example
These commands allow you to use Python 3 or Python version 2 according to your needs.
Installing a Specific Version of Python in Termux
You can install any version of Python you want by entering its version.
For example:
To install Python v3.9.4, you can enter the following command in Termux.
apt install python=3.9.4
With the above command, the specified version of Python will be installed for you.
Also, if this version is not available, you will encounter an error.
Why Use python2 When Python 3 is Available?
Many programs are written using python2, and if someone tries to run those programs in python3, they will receive errors such as the Indentation error.
For this reason, to run programs written in Python 2, it must be installed in Termux.
How to Upgrade Installed Packages for Python Development in Termux
If you installed this application a year ago and now want to upgrade the installed packages again, you should type and execute the following code in the command typing section:
pkg update
pkg upgrade
How to Search in Termux
To find specific packages in the Termux repository, you need to be able to call the command of interest. The specific command to search for a particular package, like Python, is as follows:
pkg search <pkg-name>
Example:
pkg search <python>
How to enable access to the mobile SD card storage for Python work in Termux
To store Termux packages and commands on your phone, you need to grant read access to the mobile SD card memory. There are two ways to enable access: the first is to type the command termux-setup-storage, and the second is to go to the Apps section, find the Termux software, and select the desired option in the application permissions section.
How to Write Your First Python Script on Your Phone with Termux
For working with Python in Termux, we plan to provide you with the first script that you can type and call on your phone. To create a Python script file, after running the software in the directory, type the following commands:
print("Hello, World")
name = input("What's your name? ")
print("Enjoy Python programming on Termux", name)
If you want your file to be saved, hit the Ctrl button in the file and then type O. After that, press Enter. If you want to exit from nano script.py, hit Ctrl and X. If you are using Python version 2, to run the script, you need to write and execute the command python2 script.py.
How to uninstall Python from Termux
If you are not satisfied with the software and no longer plan to work with Python in Termux, you can uninstall Python version 2 by writing the command pkg uninstall python2. If you have installed Python 3, you need to type and call the command pkg uninstall python3.
Termux vs. Linux: What’s the Difference?
Linux, like Windows, which is made by Microsoft, or Android, which is made by Google, is a type of operating system. Linux is installed on desktops; however, Termux is an application for Android users.
I hope that by reading this tutorial, you have found what you were looking for. I also recommend that you visit other articles about Termux on our website.