Where do I find the python standard library code?

Posted on

Question :

Where do I find the python standard library code?

I wanted to try and look up the source of some of the modules in the Python standard library, but wasn’t able to find them. I tried looking in the modules directory after downloading the python tarball, but it has mainly .c files. I also tried looking at the directory where the python that already comes with the OS (mac osx) has it’s modules, and there it seems to have mainly .pyc and .pyo files. Would really appreciate it if someone can help me out.

(I tried what was suggested in the question How do I find the location of Python module sources? with no luck)

Asked By: iman453


Answer #1:

In cpython, many modules are implemented in C, and not in Python. You can find those in Modules/, whereas the pure Python ones reside in Lib/.

In some cases (for example the json module), the Python source code provides the module on its own and only uses the C module if it’s available (to improve performance). For the remaining modules, you can have a look at PyPy’s implementations.

Answered By: phihag

Answer #3:

You can get the source code of pure python modules that are part of the
standard library from the location where Python is installed.

For example at : C:Python27Lib (on windows) if you have
used Windows Installer for Python Installation.

Answered By: sateesh

Answer #4:

Look it up under the Lib sub-directory of the Python installation directory.

Answered By: Abderahman Issack

Answer #5:

That would depend on what you define as Standard Library.

The Python Documentations says:

…this library reference manual describes the standard library that is
distributed with Python. It also describes some of the optional
components that are commonly included in Python distributions.

Python’s standard library is very extensive, offering a wide range of
facilities as indicated by the long table of contents listed below.
The library contains built-in modules (written in C) that provide
access to system functionality such as file I/O that would otherwise
be inaccessible to Python programmers, as well as modules written in
Python that provide standardized solutions for many problems that
occur in everyday programming. Some of these modules are explicitly
designed to encourage and enhance the portability of Python programs
by abstracting away platform-specifics into platform-neutral APIs.

If you take an extensive criteria, the Python Documentation explicitly answers what you’re asking for, and I quote:

Exploring CPython’s Internals.

CPython Source Code Layout.

This guide gives an overview of CPython’s code structure. It serves as a summary of file locations for modules and builtins.

For Python modules, the typical layout is:


For extension-only modules, the typical layout is:


For builtin types, the typical layout is:


For builtin functions, the typical layout is:


Some exceptions:

builtin type int is at Objects/longobject.c
builtin type str is at Objects/unicodeobject.c
builtin module sys is at Python/sysmodule.c
builtin module marshal is at Python/marshal.c
Windows-only module winreg is at PC/winreg.c
Answered By: Guzman Ojero

Leave a Reply

Your email address will not be published. Required fields are marked *