44

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)

Community
  • 1
  • 1
iman453
  • 9,105
  • 16
  • 54
  • 70
  • oh haha, I read somewhere that a good way to learn how good python code is written is to read up on the standard library code. I guess I misunderstood. It's all in C? – iman453 Jul 08 '12 at 18:32
  • Performance critical parts? Sure, we want performance after all. It's not bad C code though ;) Still enough python written stuff in cpython. – Voo Jul 08 '12 at 19:39

6 Answers6

45

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.

mnme
  • 620
  • 6
  • 19
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Ah, there it is. I should have looked around more before asking the question. Thanks! Will accept the answer once the time limitation lets me. – iman453 Jul 08 '12 at 18:35
  • Why do you mention PyPy? Unless the questioner is actually on PyPy, they're not going to be using the PyPy implementations. – user2357112 Jan 12 '19 at 19:22
  • @user2357112 Unless is questioner is actually on cpython, they're not going to be using the cpython implementations either. And even if they are on cpython: As written in the answer, some modules in the cpython standard library implementation are written in C, not Python. The questioner explicitly asked about that (`I tried looking in the modules directory after downloading the python tarball, but it has mainly .c file`). Not everybody can program in C; some people start learning programming in Python. – phihag Jan 12 '19 at 23:16
2

The canonical repository for CPython is this Mercurial repository. There is also a git mirror on GitHub.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
  • 1
    I'm not sure whether this answers the OP's question. @iman453 already has a tarball; their question is about which *files* in that tarball are interesting. I'm also a little bit puzzled about this answer since I believe I have already pointed to the Mercurial repository in [my accepted answer](http://stackoverflow.com/a/11385592/35070), although I did not mention the GitHub mirror. – phihag Aug 02 '16 at 14:50
  • 5
    GitHub is now the source of truth (see [PEP 512](https://www.python.org/dev/peps/pep-0512/)). – Martijn Pieters May 06 '17 at 16:24
2

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:

Lib/<module>.py
Modules/_<module>.c 
Lib/test/test_<module>.py
Doc/library/<module>.rst

For extension-only modules, the typical layout is:

Modules/<module>module.c
Lib/test/test_<module>.py
Doc/library/<module>.rst

For builtin types, the typical layout is:

Objects/<builtin>object.c
Lib/test/test_<builtin>.py
Doc/library/stdtypes.rst

For builtin functions, the typical layout is:

Python/bltinmodule.c
Lib/test/test_builtin.py
Doc/library/functions.rst

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
Guzman Ojero
  • 2,812
  • 1
  • 20
  • 20
0

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:\Python27\Lib (on windows) if you have used Windows Installer for Python Installation.

sateesh
  • 27,947
  • 7
  • 36
  • 45
0

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

0

The source code for many standard library packages is linked at the top of the package's documentation page in the library documentation, for example, the docs for the random module.

The original commit message for adding these links states

Provide links to Python source where the code is short, readable and informative adjunct to the docs.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153