There are ways to use the Python programming language on a regular Android device, iPhone, or iPad, but with fewer features available than Python on most desktop computers. Fortunately, you can still get the full Python experience on these devices: you just need a Raspberry Pi or other low-power server on the same network.
You can set up a Python development environment as a Raspberry Pi and then use it on any other computer, phone, or tablet on your network using SSH sessions or remote desktop. An SSH session gives you a similar experience to running Python in any terminal or command prompt, although the editing experience is not ideal on most phones and tablets. A remote desktop session works just like your Pi’s own display, with added latency and video compression.
This guide is written primarily for a Raspberry Pi running the Raspberry Pi operating system (formerly called Raspbian). The instructions should also work for most devices running Debian or a Debian-based distribution like Ubuntu. Your mileage may vary if your Pi is running a different operating system.
Configuring Python, SSH, and Remote Desktop
We need to configure three components: Python environment, SSH access and remote desktop. Some or all of these may already be enabled on your Raspberry Pi, but we’ll check anyway.
First of all, you need to open the terminal. If the Pi is connected to a TV or monitor or a remote desktop session, open the main menu, then go to Accessories > Terminal. If you’re using the Pi over an SSH connection, you’re already there.
Type this command to refresh your package repositories and install all available updates, then press Enter/Return on the keyboard to run it:
sudo apt update && sudo apt upgrade
Next, run this command to install Python3, if it is not already installed:
sudo apt install python3 -y
Then you can run this command to verify that Python is working and check the exact version:
python3 –version
Next, let’s configure SSH and VNC remote access with the built-in raspi-config tool. You can skip this step if you know that both features are already enabled or if you have configured your own alternatives. Run this command to open the configuration tool:
sudo raspi-config
Using your up and down arrow keys, navigate to “Interface Options,” then select it with the Enter/Return key.
This is the menu with settings to enable SSH and VNC. Select the SSH menu option and follow the prompts to enable it, then do the same for VNC.
When you’re finished, return to the main menu (if you’re not automatically sent there), then use the left and right arrow keys to select the “Finish” button. The configuration tool will close with your changes saved.
Finally, you need to know your username on the Pi, as well as the IP address of the Pi on the network. If you’re not sure, run this command to list all network connections:
ifconfig -a
Your IP address will be listed under the connection type, after the “inet” label, and probably starting with 192. My Raspberry Pi is connected via its built-in Wi-Fi connection (called “wlan0”) and the IP address is “192.168.50.157”.
You can use this command to see the current username:
whoami
Although not required, you may also want to configure Samba/SMB for remote file access, so you can easily move Python files between your Raspberry Pi and other devices on a network. Moving files with flash drives also works.
Programming with SSH
If you’re comfortable with the terminal interface, using Python over an SSH session might be the best option. You get the lowest possible latency with crisp graphics, and you can use any number of terminal-based text editors while working on files. It’s a great option for quickly trying out Python code snippets, although most people will want a GUI with code completion, debugging, and other useful features.
First, you need an SSH client on the device you want to use for Python programming. There’s one already built into Windows, macOS, and Linux, as well as Chromebooks when you enable the Linux container, but those devices can usually run Python natively as well. On iPhone and iPad you can try Prompt 3 Or Terminus. The Termux app is a great option for Android devices.
If you are using a terminal application with the SSH command, simply run this command in the terminal, replacing “pi” with your Pi’s user account name and the number with your Pi’s IP address:
ssh [email protected]
The SSH client will ask you to accept the encryption key, then you should be connected to your Pi.
In other SSH applications, there is usually an interface to add a new host connection where you enter the same information. After setting up the Terminus app on my iPad Air, I can tap the “Add Host” button and enter my Pi’s IP address, account, password, and nickname. Once connected, I get a standard terminal interface.
If you just want to try out some Python code snippets, you can run python3 in the terminal to open the interpreter. You can enter lines of code to run and use the exit() command when you’re done.
For more complex projects, you can create a folder for your project and use Nano or another text editor to work on the files. For example, this creates a project folder in your home directory:
mkdir ~/project1
Then move to this folder:
cd ~/project1
Next, create a new file called app.py in the directory with the Nano text editor (or another editor if you prefer):
nano app.py
This will open the Nano text editor, where you can start typing a program. When finished, press Ctrl and X simultaneously on the keyboard (some SSH clients have a Ctrl modifier key in the interface), then press Y to save, and finally Enter/Return to confirm.
To run the file you just created, run this in the terminal:
python3 app.py
If you start working on more complex code, you should create a Python virtual environment in this folder, so that your installed libraries stay in this project and do not conflict with other Python software or project directories.
Programming with Remote Desktop
The other option for remote programming with a Raspberry Pi is Remote Desktop, which displays your Pi’s desktop environment via any device that has a VNC client installed. It’s like connecting the Pi directly to a monitor or TV, but the screen is displayed in a window and there is additional latency. You can use any of your favorite code editors, not just command line tools.
You need a VNC client on the device you will connect to the Pi. TigerVNC is an option for Windows, Mac and Linux. RealVNC has iPhone, iPadAnd Android versions available.
After installing the client, you will need to enter your Pi’s IP address and a nickname. After the initial connection, the VNC client will ask for your Pi’s username and password. The Pi desktop should eventually load.
In this desktop environment, you can use any graphical text editor or IDE of your choice. Gearny is the built-in editor for Python in recent versions of the Raspberry Pi operating system.
With SSH or VNC, you can work on Python projects on devices that can’t run Python code directly, like an iPad. You’ll still get a better experience working directly on the Pi, but it’s more convenient than some cloud-based programming editors or limited IDEs designed for mobile use.