RasPi Banner

Python

Python is a powerful, and yet simple, programming language which is very popular with Raspberry Pi users as well. It is native in all Raspberry Pi images.

This page lists our Python tutorials, and some important links for solving various tasks during our development.


Please note: there are literally thousands of opinions on which Python version to use. We will not commit to this game. For the Raspberry Valley makerspace, we will use Python 3 (Python 2 is coming to an end anyway and essentials libraries are upgraded) and we assume that you have setup the pre-requisites. It's a good idea to visit our guide: Python Development Environment, to setup a development environment. This guide works for your recent Raspbian, as well as other systems; Ubuntu or even Windows Subsystem for Linux.


Please find below interesting Python links, broken down into categories

Python Banner

Introductions

Our Python Articles

We have Python scattered basically all over this site. However below are some of our notable articles using Python features

Essential Tools

Tools we use for good Python development.

Linters:

  • Pylint - Makes sure the code conforms to Pep 8 Style Guide. Note that Visual Studio Code has this linter set as default for the official Python extension
  • Prospector - for a long time, a tool of choice. See usage tips below

Other:

Libraries

  • inputs - Cross-platform Python support for keyboards, mice and gamepads
  • curses-menu - A simple Python menu-based GUI system on the terminal

Jupyter and JupyterLab

Doing data science, experimenting with Python code (or R or Julia code), trying out things in Python, Documenting your approach - all of this and more is covered by Jupyter. Check our Jupyter page to learn more.

Apart from Jupyter, we have also a more advanced tool and a successor: JupyterLab.

JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. We find using Jupyter for Python development and education great, so here are a few interesting links.

jupyter notebooks

Anaconda

A famous environment for developing Python solutions, with a focus on data science, is Anaconda. If you want to get started with the most important Python libraries and are happy to use an IDE, Anaconda is the place to go. Below is the Anaconda Navigator, to give you a feel of the IDE.

anaconda navigator

Prospector

Prospector is a tool to analyze your Python code and give you an overview about errors and convention violations. This is a good addon for your development. The idea is to install Prospector, then run it once in a while in your project directory to get an analysis of your code compliance with good coding standards.

You can install Prospector using pip.

pip install prospector

And then run it in your project directory:

prospector

Tutorials

Tips

Find some nice tips on using Python in this section.

Serving HTML Pages

You are developing the next big website and don't want to install IIS, and want to serve your pages to see how they look: try using a pre-installed Python web server!

  • Navigate to the folder where you have your HTML files and scripts, using the terminal
  • Execute the following command (1234 is the port number you wish to use):
python -m http.server 1234

Note: if you for any reason use Python 2.x, run the following instead:

python -m SimpleHTTPServer 1234
  • access your files in your browser using this URL:
http://localhost:1234

Upgrading pip

You might get warnings on an outdated pip version. Just follow the instructions, they are pretty clear.

To upgrade pip, type the following:

python -m pip3 install --upgrade pip3

If you have only Python 3 installed, chances are that you need to upgrade as shown below. Note, that this doesn't apply to Raspbian (at the time of this writing), as Python 2 is still part of the distro.

python -m pip install --upgrade pip

Removing all packages

You might have decided to use virtual environments, as described in our Python Development Environment article - if you haven't already, we highly recommend this. Or simply want to remove packages which have accumulated over time.

Here is how. This recipe works on Linux systems, Windows and Mac as well.

First, fire up your terminal, and get a list of installed packages:

pip freeze > requirements.txt

To remove all packages, you now type the following:

pip uninstall -r requirements.txt -y

Or, if you wish to run an interactive session and confirm/decline the uninstallation of each package, type this instead:

pip uninstall -r requirements.txt

Alternatively, without creating the intermediate list requirements.txt, simply type this (non-interactive) one-liner:

pip uninstall -y -r <(pip freeze)