import a function from another .ipynb file

后端 未结 2 2050
面向向阳花
面向向阳花 2020-12-15 02:58

I defined a hello world function in a file called \'functions.ipynb\'. Now, I would like to import functions in another file by using \"import functions\". I am sure that th

相关标签:
2条回答
  • 2020-12-15 03:29

    You'll want to use the ipynb package/module importer. You'll need to install it: pip install ipynb.

    Create a Notebook named my_functions.ipynb. Add a simple function to it.

    def factorial(n):
        if n == 0:
            return 1
        else:
            return n * factorial(n-1)
    

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

    from ipynb.fs.full.my_functions import factorial
    

    Then you can use it as if it was in the same IPython Notebook:

    testing = factorial(5)
    

    See the documentation for more details.

    0 讨论(0)
  • 2020-12-15 03:35

    You can save functions.ipynb as functions.py and can import the file as 'import functions'. Now you can use any function defined in the functions file as 'functions.function_name' For eg, if add is a function, functions.add(5,3) after importing will work.

    0 讨论(0)
提交回复
热议问题