Check source code of Python Function or Module

Qianjiang Hu
2022-01-01

Python is a popular programming language,being of a large opensource presence with many powerful projects that receives contribution from developers around the world.

However, current developed packages/modules can not always meet the requirement for data analysis or project develping. It is quite common that we need to modify and adatp the current functions/modules into our project. On the other hand, checking the source code can garantee a clear understanding of the function we are using.

Via sys.modules files

Step 1: The module whcih the function belongs to
Step 2: The path of the module and function files
Step 3: Check the .py file to get the source code

Via functionNama??

Via inspect

import inspect

source = inspect.getsource(func)

Via Pycharm