FREE SHIPPING WITHIN THE USA
30-Day Easy Returns 30-Day Easy Returns
Secure Checkout Guaranteed Secure Checkout Guaranteed
Free Shipping in USA Free Shipping in USA

Your Cart 0 Items

Shipping: Free
Sub Total: $0.00

Setting Up Python on Raspberry Pi

There’s plenty to do with a Raspberry Pi and the various apps and bits of software you can run on it…but there is a special joy in creating your own software. A Raspberry Pi is a great machine for tinkering with programming and crafting your own bespoke software.

 

Python is a programming language that is powerful, flexible, and relatively easy to learn. Best of all, it is enormously popular, so there is a plethora of resources and and support across the internet.

 

Let’s take a look at using Python on your Raspberry Pi.

 

SETTING UP PYTHON

Python comes pre-installed on your Raspberry Pi, so you shouldn’t have to do any sort of set-up or installation. To make certain your Pi has Python on it, simply open the Terminal and type python3.

If you instead receive some sort of error, then you’ll need to install Python after all. Buckle up — this is an involved process and one that requires several steps. Please read and follow these directions carefully!

1. Preparation

It’s always best to update your system before installing something new, so run the following command in Terminal -

sudo apt-get update 

Then run the following (again, in Terminal) to make certain you have the software necessary to build and install Python:

sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev

2. Download Python

Switch to your Pi’s web browser. Visit https://www.python.org/downloads/ and click on the button to download the latest version.

3. Extract & Configure Python

In the Terminal, navigate to your Downloads folder. You can do this with the command

cd ~/Downloads.

Next, run the ls command to get the name of the file you just downloaded. It will be a tar.xz file with the word python in there. For example, it might be Python-3.14.4.tar.xz.

Once you have the name of the file, you will extract the files. (If your file is named something other than “Python-3.14.4.tar.xz,” then substitute your name in the command below).

tar -xvf Python-3.14.4.tar.xz

Now the requisite files have been extracted from the original file into a new directory called “Python-3.14.4” (or whatever name your file had).

Go to this new directory with cd Python-3.14.4 and run a new command, one that will configure your upcoming Python build: ./configure --enable-optimizations

4. Build & Install

Now issue the command make, which will build Python for you. Once that’s done, issue the command sudo make altinstall.

Please be aware: These processes can take a very long time, even on a powerful Pi! It may take fifteen minutes or more for the process to finish!

Now you’ve installed Python on your Raspberry Pi.

5. Upgrading Python

Python is regularly updated, so if you want to be running the latest version, you’ll need to perform a manual upgrade. To do so, you’ll follow the instructions in the section above on Installing Python, then continue on here:

Once you’ve installed the newer version, you have a problem: You have two versions of Python on your system! This isn’t a problem for the Pi, but it can be a problem for you, since now your Pi defaults to the older version.

To fix this, you’ll edit a special link on your system that tells your Pi which version of Python to use a default. Follow these steps in Terminal:

   cd /usr/bin

   sudo rm python

   sudo ln -s /usr/local/bin/python3.14.4 python (remember to replace the version numbers “3.14.4” with the appropriate ones from the version you’ve downloaded!) 

Then run python3 —version. You should see your new version number reflected.

USING PYTHON

It’s possible to write Python code in the Terminal, but most people find it easier to use an IDE, an Integrated Development Environment. This is an app that programmers use to write code, and it makes the task a lot easier.

There are two different IDEs already installed on your Pi: Geany and Thonny.

Which one you use depends on your individual preferences.

Once you’ve written a script in Python, it can be run from the IDE or even from the command line. To run your Python script, you simply type python3 followed by the path to your script. So, if you have a script named hello_pi.py in the Scripts directory of your Documents directory, you would type

python3 ~/Documents/scripts/hello_pi.py

MORE LEARNING

If you’d like to learn more, we recommend the following sites:

   https://raspberrytips.com/python-tutorial-raspberry-pi/

   https://www.raspberrypi.com/resources/learn/

   https://editor.raspberrypi.org/en-US/