Sergey Antopolskiy
Sergey Antopolskiy

Reputation: 4290

Import Jupyter notebook as a module: path problems

I am trying to load some of my Jupyter notebooks as modules. Jupyter has nice instructions on how to do it here. I have copied all the necessary functions and classes in the notebook_import.py (full script on github) and I load everything from it. Then I can import notebooks and it works:

# import functions and classes to make notebooks importable
from notebook_import import *

The following notebook contains test function foo() that prints "bar"

import notebook_import_test
[out]: importing Jupyter notebook from notebook_import_test.ipynb

notebook_import_test.foo()
[out]: bar

However, for some reason this only works if the notebook I am trying to import is in the same folder as the one to which I am importing. If I put the notebook in my usual folder for modules (i.e. the one where I put my *.py files with functions to load them later as modules), it says No module named notebook_import_test. I am not savvy enough with path management and how Python looks for modules to figure out how can I make this work for notebooks as for *.py imports, so that notebook load from another folder, dedicated to modules.

Any suggestions?

P.S. My OS is Win10.

Upvotes: 1

Views: 2295

Answers (2)

bkuriach
bkuriach

Reputation: 350

Simple Solution with 2 lines of code

Use 'nbimporter' package in python for importing another notebook A (or its function) in notebook B. You can install 'nbimporter' by using the command - pip install nbimporter

Assume there are two Notebooks A.ipynb and B.ipynb. we are trying to import Notebook A (or its function) inside B Code sample below should solve the issue.

Inside Notebook B.ipynb

import nbimporter
import A  # or do this --> from A import func1

Upvotes: 2

kikocorreoso
kikocorreoso

Reputation: 4219

[I add this as an answer because as a comment the formatting is not enough to explain my comment. I'm guessing so maybe it is not a good answer].

I suppose you are doing so from other notebook. If you started jupyter on a folder (you are on win10), e.g., inside folder:

C:\my_notebooks

And the notebook you want to import is on another folder not in the C:\my_notebooks folder, e.g., C:\dir_with_the_nb_to_import:

C:
├───my_notebooks       # Here you starter jupyter, > jupyter notebook
│   ├───notebooks      # so you have access to all the info in the folder
│   │   └───data
│   └───utils
├───dir_with_the_nb_to_import    # this folder is not inside my_notebooks
    └───notebook_to_import.ipynb # so maybe the problem is that it is not accesible

then jupyter doesn't allow you to find the file as you only have access to files included in the place you started your jupyter server.

I don't know if including C:\dir_with_the_nb_to_import to your sys.path would help in this case:

import sys
sys.path.append(r'C:\dir_with_the_nb_to_import')

[EDIT]: After looking into your linked file it seems that changing this line in the following way:

Before:

for d in path:

After:

for d in sys.path:

This way it will look for notebooks into the sys.path so you should add the place where the notebooks lie to your sys.path.

Upvotes: 1

Related Questions