0

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 ?

6
  • 1
    github.com/python/cpython Commented Aug 2, 2014 at 14:13
  • @Sneftel Put a bit of explaining text around it, and it’s the correct answer to this question. Commented Aug 2, 2014 at 14:13
  • @Sneftel Note that this is a semi-official mirror (admittedly with a nicer UI), the authoritative source is a Mercurial repository at hg.python.org/cpython Commented Aug 2, 2014 at 14:16
  • @delnan Yeah, but the Hg web interface is truly execrable compared to Github's. Worth the semi-officiality. ;-) Commented Aug 2, 2014 at 14:19
  • 1
    @Sneftel When I have to look up more than one thing in a week, I clone the repository and look through it locally anyway (ack-grep, find, and the like beat any web interface). Commented Aug 2, 2014 at 14:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.