74

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 they are in the same folder. However, it still shows that "ImportError: No module named functions". By the way, I am using jupyter notebook. Thanks a lot!

Chao Song
  • 741
  • 1
  • 5
  • 3
  • 4
    Possible duplicate of [ipynb import another ipynb file](https://stackoverflow.com/questions/20186344/ipynb-import-another-ipynb-file) – mx0 May 22 '17 at 15:02

5 Answers5

99

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.

drhagen
  • 8,331
  • 8
  • 53
  • 82
David Rinck
  • 6,637
  • 4
  • 45
  • 60
  • 1
    This does not work for me I always get this error KeyError: '__package__'. The environment I use for notebooks is Azure Databricks and python 3. Is there a solution to fix this? – HEART94 Feb 06 '20 at 23:16
  • Make sure you install the package ipynb otherwise it will not work! – Koen Feb 25 '20 at 16:06
  • 12
    What if `my_functions` contains some code that I don't want to execute, but only want a function from the file? – Milos Aug 18 '20 at 19:42
  • ModuleNotFoundError: No module named 'ipynb.fs.full.my_functions' I am getting this error,Can someone advice the cause for that,,I installed ipynb as well,,, – suresh_chinthy Dec 09 '20 at 05:04
  • @suresh_chinthy Make sure that all the notebooks are in the same directory. – jitin14 Dec 28 '20 at 12:53
  • 9
    @Milos then use `import ipynb.fs.defs.my_functions` as documented here https://github.com/ipython/ipynb – racoon_lord Jan 11 '21 at 18:17
  • @racoon_lord I am using the same syntax to import only function definitions. While this is working in .ipynb files. For some reasons I have to convert my second python notebook file to .py format. But this time, it is executing code of first Notebook as whole (including the code that I do not want to execute) instead of only function I want to call. Any solution to this? – Programmer Aug 16 '21 at 20:04
  • Actually I am performing Machine learning in first file and from second file I am executing function to check predictions. (Do no want to run training part again). – Programmer Aug 16 '21 at 20:05
9

For my use case the ipnyb import didn't work for some reason. I had to use Jupyter Notebook magic cell to import my function.:

%run MyOtherNotebook.ipynb     #this is were my function was stored
function(df)                    #then simply run the function
Gedas Miksenas
  • 959
  • 7
  • 15
5

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.

Zoi
  • 7
  • 3
Tinto Raj
  • 51
  • 1
  • 1
4

@David Rinck's answer solved the problem, but I'd like to recommend you add the boilerplate __name__ == "__main__" to guard the scripts you don't want to accidentally invoke. It works the same way as in a usual Python file.

If a .ipynb file a.ipynb is imported by another one b.ipynb

from ipynb.fs.full.a import factorial

the __name__ in a.ipynb would be ipynb.fs.full.a rather than "__main__".

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
1

You can use the solution provided by @David Rinck but beware that the ipynb file that you are importing the function from will run as a whole. So it is advisable that the file does not have any code the runs and only has functions that you may need. I tried it myself and the same thing happened to me. An easy solution that I resort to is just make a .py file containing the functions that I need and just import the functions from that file.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33937256) – xlmaster Mar 04 '23 at 12:19