Faster Python apps
Faster Is Better
![© Lead Image © Tomasz Pacyna, 123RF.com © Lead Image © Tomasz Pacyna, 123RF.com](/var/linux_magazin/storage/images/issues/2021/246/pypy-and-nuitka/123rf_32604571_steampunk-train_tomaszpacyna_resized.png/785004-1-eng-US/123rf_32604571_Steampunk-train_TomaszPacyna_resized.png_medium.png)
© Lead Image © Tomasz Pacyna, 123RF.com
PyPy and Nuitka improve the performance of Python on a Raspberry Pi.
Python apps on lower-end hardware like Raspberry Pis can be a bit slow, but luckily you have some options that can improve their performance. A number of interesting packages allow you to compile, interpret, or repackage your Python apps. In this article, I highlight two packages that have given me good success:
- PyPy [1] – a replacement to the native Python interpreter that runs more than four times faster
- Nuitka [2] – a native Python utility to compile Python apps to C code
How a Python application performs is based on a lot of different factors, so it's important to know the limitations and how best to work with them. PyPy and Nuitka might not work for all applications, but for many common types of projects they are great fits.
Background
PyPy has been around for a while, and Guido van Rossum, the creator of Python, is popularly said to have stated: "If you want your code to run faster, you should probably just use PyPy."
The PyPy site has some performance results, and they state that, on average, PyPy will run 4.2 times faster than native Python. In one of my test projects, it ran nine times faster than native Python. PyPy really shines in the area of web services, database connections, and iterative (large loop) applications.
It's very important to note that PyPy only supports a limited number of Python libraries. For Raspberry Pi projects, two of the important libraries that PyPy does not support are tkinter, a graphic interface library, and RPi.GPIO, the Raspberry Pi general purpose I/O library. To see whether PyPy will work with your application, check the supported libraries [3].
Nuitka will not give the same performance results as PyPy, but it can offer some speed improvements over native Python. Nuitka supports a large majority of the Python libraries, so it's a good fit when you can't use PyPy. Nuitka has the added benefit of creating a compiled application, so you don't need to distribute Python source code.
Figure 1 summarizes the project requirements best served by PyPy and Nuitka.
PyPy – A Faster Python
The improved performance of PyPy is due to its just-in-time compiler, as opposed to the native Python's line-by-line interpreter.
PyPy has a version for Python 2.7 and 3.6 for Linux, macOS, and Windows. The PyPy version for Python 2.7 is preinstalled on the latest Raspbian operating system for the Raspberry Pi. To install the PyPy3 version in Ubuntu, enter:
sudo apt update sudo apt install PyPy3
The nice thing about PyPy is that you can use your base Python code as is, and you can do basic testing with PyPy in command-line mode. For example, on the Raspberry Pi, if I want to check whether some basic Python libraries are loaded and then see if the Pi GPIO library is supported, I would use the pypy3
command (Listing 1).
Listing 1
Checking PyPy Support
§§noumber $ pypy3 Python 3.5.3 (7.0.0+dfsg-3, Mar 03 2019, 06:11:22) [PyPy 7.0.0 with GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>> import os >>>> import time >>>> import RPi.GPIO Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/RPi/GPIO/__init__.py", line 23, in <module> from RPi._GPIO import * ImportError: No module named 'RPi._GPIO' >>>>
To run your Python application from the command line, substitute python
with PyPy
(or PyPy3
):
$ PyPy3 myapp.py
If you are running your Python app as an executable script (i.e., as chmod +x myapp.py
), then you'll need to change the first line of your script from #!/usr/bin/python
to #!/usr/bin/PyPy3
.
PyPy Libraries
By default, only the basic Python libraries are installed with PyPy. To load a specific Python library into PyPy3, you need to install the Python package installer (pip
) module before Python packages can be loaded:
$ wget https://bootstrap.pypa.io/get-pip.py $ PyPy3 get-pip.py $ PyPy3 -m pip install <some-pymodule>
I found I was able to load some of the "lighter" modules (e.g., bottle, requests, beautifulsoup4, pika, etc.) without any issues. However, some of the "heavier" modules (e.g., NumPy and MatPlotlib) would not load directly. If you're planning on using some of these modules, the recommendation is to run PyPy in an isolated Python environment (virtualenv
) [4].
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.