Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check source code of a module in Jupyter notebook?

For example, I want to check the source code of a python library, directly in the notebook, is there a way to do that?

Thank you

like image 344
user697911 Avatar asked Dec 02 '16 21:12

user697911


People also ask

How do I get the source code of a module?

We use the getsource() method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.

How do you view source code in Python?

getsource() method is used to get the source code of Python objects. An IOError is raised if the source code cannot be retrieved. Example: We can use inspect on built in library functions and objects also.

How do you run a source code in Jupyter Notebook?

To run a piece of code, click on the cell to select it, then press SHIFT+ENTER or press the play button in the toolbar above. Additionally, the Cell dropdown menu has several options to run cells, including running one cell at a time or to run all cells at once.

How do I get the Python code from Jupyter Notebook?

Open the jupyter notebook that you want to convert. Navigate into the 'File' menu and select 'Download as'. The more options will be displayed in the form of a list where you will click on the 'Python (. py)' option.


2 Answers

Type the fully qualified function name, then type ??.

like image 154
Charlie Avatar answered Oct 07 '22 19:10

Charlie


You can use inspect module (which is built in) - for example you can see the tree module below from scikit-learn.

import inspect from sklearn import tree inspect.getsourcelines(tree.tree)  
like image 38
Chinny84 Avatar answered Oct 07 '22 18:10

Chinny84