There is a very detailed guide to get started with CPython from the official documentation that can provide more context and details on each step.
To simplify the process, make sure you have the following dependencies in your computer:
- Git (optional, but recommended)
- A C-compiler:
gccfor Linux,clangfor macOS (via XCode),clfor Windows (via MSVC)
- Your favorite editor or IDE.
Tip
On a fresh machine, ./configure can fail if some development headers are missing. A quick
preemptive install:
- Debian/Ubuntu:
sudo apt install build-essential gdb lcov pkg-config libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev - macOS (Homebrew):
brew install openssl xz gdbm - Windows: dependencies are handled automatically by
build.bat
Tip
Some exercises later in the tutorial need extra tools. Installing them now avoids waiting on slow downloads mid-session:
It's recommended that you clone the cpython repository
so you can access the code that we will use:
git clone https://github.com/python/cpython
in case your Internet is not so good, you can do a clone of the last commit from the repository (also called shallow clone) by using:
git clone --depth=1 https://github.com/python/cpython
Important
These commands clone the main branch by default, which is what the exercises assume.
Some exercises (e.g. the argument clinic output in Exercise 04)
rely on recent additions to CPython, so checking out an older release tag/branch instead of
main may leave you with different code than what's shown in the exercises.
You are now prepared to start the tutorial!
There are two main sections on this tutorial, each one will be followed by a couple of exercises, which you can find in the exercises directory.
You can access them directly from here:
- Compile a C Program
- Compile CPython
- Add a Python function to CPython
- Add a C function to CPython
- Add a new method to a Python container
- Add a new grammar alias to CPython