showkey
showkey

Reputation: 358

How to get the source code of built-in module?

The source code of module or function can be get from inspect .

import inspect
print(inspect.getsource(moduleName.function))

What about if the module is a built_in module?

import sys
print(inspect.getsource(sys))

TypeError: <module 'sys' (built-in)> is a built-in module .

Where can i get it then ?

Upvotes: 0

Views: 472

Answers (1)

poke
poke

Reputation: 387647

The sys module is one of the very few ones which are not implemented in Python itself, but in native code instead. The C source is located in /Python/sysmodule.c.

Most of the other modules are directly implemented in Python. You can find the source for those in your lib directory of your Python installation, or also in the source.

Upvotes: 1

Related Questions