View By

Categories

 

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__
'/Users/Rajput/anaconda/lib/python2.7/lib-dynload/math.so' 
>>> random.__file__
'/Users/Rajput/anaconda/lib/python2.7/random.pyc'

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',
  '\n',  
'    integers\n',  
'    --------\n',  
'           uniform within range\n',  
'\n',  
'    sequences\n',  
'    ---------\n',  
'           pick random element\n',  
'           pick random sample\n',  
:
so on..


21 views

Help others, write your first blog today! 

Home   |   Contact Us

©2020 by Data Nebulae