How can I get the source code of the Python Packages?

You can find source code on Github. Under cpython you will be able to find all the modules and python objects (written in C).

You can also find which file (using __file__ attribute) on your system is used for respective module. For example math and random module,

>>> import math
>>> import random 
>>> math.__doc__
'This module is always available.  It provides access to the\nmathematical functions defined by the C standard.' 
>>> math.__file__
>>> random.__file__

Now, you can get source code for objects written in python (not C) with inspect get source code. You can also refer Python docs for “inspect” library.

>>> import inspect
>>> inspect.getsourcelines(random) 
(['"""Random variable generators.\n',
'    integers\n',  
'    --------\n',  
'           uniform within range\n',  
'    sequences\n',  
'    ---------\n',  
'           pick random element\n',  
'           pick random sample\n',  
so on..

32 views0 comments

Help others, write your first blog today!