Home > Enterprise >  How to call a function from one Jupyter notebook to another in Visual Studio Code
How to call a function from one Jupyter notebook to another in Visual Studio Code

Time:11-09

I would like to create a python pandas project in Visual Studio Code that consists of Jupyter notebooks. I would like to have a notebook where I store all my functions and be able to call them in other notebooks where I am exploring a specific dataset. How can I do that? If I am using Jupyter notebooks in the browser I would say %run .ipynb. However, this does not work in VS code.

CodePudding user response:

I can only give you an abstract example because you didnt include any code examples.

Use the ipynb package/module importer. pip install ipynb

Create a notebook called master_functions.ipynb.

You can add all you functions here.

def master_function1(arg):
    print('hello', arg)

......
.....
.....

def master_function10(arg):
    pass

Then, create a second IPython Notebook and import this function with:

Now you can do this, which can import individual functions,

from ipynb.fs.full.master_functions import master_function1

master_function1('world') # prints hello world

You can also do this which imports master_functions as mf,

from ipynb.fs.full.master_functions as mf

mf.master_function1('world') # prints hello world

And this which imports all the functions from master_functions.ipydb

from ipynb.fs.full.master_functions import *

master_function1('world') # prints hello world
  • Related