Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

How to use Python on an Android phone, iPhone, or iPad (with a Raspberry Pi)

Python and a Raspberry Pi
Corbin Davenport / How-To Geek

There are ways to use the Python programming language on a typical Android device, iPhone, or iPad, but with fewer features available than Python on most desktop computers. Thankfully, you can still get the full Python experience on those 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 that on any other computer, phone, or tablet on your network using SSH sessions or remote desktop. An SSH session gives you a similar experience as running Python in any terminal or command prompt, though the editing experience isn't ideal on most phones and tablets. A remote desktop session works just like your Pi's own screen, with some added latency and video compression.

This guide is written primarily for a Raspberry Pi running the Raspberry Pi OS (formerly called Raspbian). The instructions should also work for most devices running Debian, or a distribution based on Debian like Ubuntu. Your mileage may vary if your Pi is running another operating system .

Setting up Python, SSH, and remote desktop

We need to set up three components: the Python environment, SSH access, and remote desktop. Some or all of those might already be enabled on your Raspberry Pi, but we'll check anyway.

Advertisement
Advertisement

First, you need to open the Terminal. If you have the Pi connected to a TV or monitor or a remote desktop session, open the main menu, then navigate to Accessories > Terminal. If you are using the Pi through an SSH connection, you're already there.

Opening the Terminal on Raspberry Pi OS

Type this command to refresh your package repositories and install any available updates, then press Enter/Return on the keyboard to run it:

Next, run this command to install Python3, if it's not already installed:

Afterwards, you can run this command to verify Python works and check the exact version:

Next, let's set up SSH and VNC remote access with the built-in raspi-config tool. You can skip this step if you know both features are already enabled, or if you have set up your own alternatives. Run this command to open the configuration tool:

Using your up and down arrow keys, navigate to 'Interface Options' and then select it with the Enter/Return key.

Interface options on a Raspberry Pi

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.

Advertisement
Advertisement

When you're done, return to the main menu (if you aren't sent there automatically), then use the left and right arrow keys to select the 'Finish' button. The configuration tool will exit with your changes saved.

SSH and VNC options on a Raspberry Pi

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:

Your IP address will be listed under the connection type, after the "inet" label, and probably starting with 192. My Raspberry Pi is connected through 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:

Even though it's not required, you may also want to set up Samba/SMB for remote file access , so you can easily move Python files between your Raspberry Pi and other devices over 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 sharp graphics, and you can use any number of terminal-based text editors while working on files. It's a great option for quickly trying Python code snippets, though most people will want a graphical interface with code completion, debugging, and other helpful features.

Advertisement
Advertisement

First, you need an SSH client on whatever 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're using a terminal app with the SSH command, just run this command in the terminal, replacing "pi" with your Pi's user account name and the number with your Pi's IP address:

The SSH client will ask you to accept the encryption key, and then you should be connected to your Pi.

In other SSH apps, there's usually an interface for adding a new host connection where you enter the same information. After setting up the Terminus app on my iPad Air, I can press the 'Add Host' button and enter my Pi's IP address, account, password, and nickname. After I connect, I get a standard terminal interface.

Adding an SSH server on an iPad

If you just want to try 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.

Using Python through SSH on an iPad

For more complex projects, you can create a folder for your project, and use Nano or another text editor to work on files. For example, this creates a project folder in your home directory:

Next, switch to that folder:

Next, create a new file called app.py in the directory with the Nano text editor (or another editor if you prefer):

This will open the Nano text editor, where you can start typing a program. When you're done, press Ctrl and X together 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.

Advertisement
Advertisement

To run the file you just made, run this in the terminal:

If you start working on more complex code, you should create a Python virtual environment in that folder, so your installed libraries stay within that project and don't 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 through any device with a VNC client installed. It's just like connecting the Pi directly to a monitor or TV, but the screen is displayed in a window, and there's some added latency. You can use any of your favorite code editors, and not just command line tools.

You need a VNC client on the device you will connect to the Pi. TigerVNC is one option for Windows, Mac, and Linux. RealVNC has iPhone , iPad , and Android versions available.

Adding a Raspberry Pi to RealVNC

After you install the client, you'll 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.

Raspberry Pi desktop in RealVNC

In this desktop environment, you can use any graphical text editor or IDE you want. Gearny is the built-in editor for Python in recent versions of Raspberry Pi OS.


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 this is more practical than some cloud-based programming editors or the limited IDEs designed for mobile use.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: