Episode #8

Let's write a Python module!

Today, I want to show you how to write a Python extension in Rust using pyo3.

I like tinkering with programming languages and making things faster. What's cooler than combining those two things? With Rust you can write safe, fast extensions for dynamically typed programming languages like Python, PHP, or Ruby. That's why today i'd like to show you how to write a Python extension from scratch!

▶ Run and edit code on Gitpod

Keywords

Tutorial, FFI, pyo3, Module, Extension, Python

Things I mentioned during the show

  • A nightly compiler is required. You can use rustup to install it.
  • To use your compiled module, run the Python interpreter outside of the project folder.
  • Here are a few ideas for your own module: Write a regex module based on Rust's re or write a str.replace method.
  • If you like this stuff check out hyperjson, a JSON module I wrote in Rust.
  • setup.py (for humans)

Errata and improvements

It might come as a surprise to you, but every once in a while even I make a mistake.
This section covers all improvements made to the code since the epsiode went live.
For an exhaustive list of all changes to the original code, go here. Thanks to all contributors!

  • Update code to work with latest version of pyo3; formatting and cleanup for stabilizations. See changes here.

Meta

  • Visual Studio Code Theme: Monokai

Resources and licenses

Support!

Preparing, recording, and editing an episode takes a substantial amount of time (around 30 hours total). I do all of this next to my fulltime dayjob. If you want to show your appreciation and help me keep the content free for everybody to enjoy, please consider supporting me on Patreon - no matter the amount. ❤️