Trying to import a simple notebook: none of the methods I have found are working


I’m pretty inexperienced with Python and Jupyter notebook but I generally manage to get by. This one has me stumped as the various solutions I have found online do not work for me. I might be missing something basic.

My goal is to use a class I’ve defined in notebook self_archiver.ipynb in notebook A.ipynb. Both notebooks are in the same directory.

Not related to the issue I’m facing, but potentially relevant to what solution you suggest, is that the class self_archiver in notebook self_archiver.ipynb uses the following method that is meant to operate on A.ipynb when run from an instance of the class instantiated in A.ipynb.

In definition of class self_archiver:

    def savenotebook(self):

The goal of the above method is to save A.ipynb.

I am on Windows 10, running Jupyter Notebook 6.4.8 by executing “Jupyter Notebook (anaconda3)” from the Start menu. sys.version says '3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]'.

sys.path is:

'D:\\<correct path to A.ipynb>',
 'C:\\Users\\<my username>\\anaconda3\\',
 'C:\\Users\\<my username>\\anaconda3\\DLLs',
 'C:\\Users\\<my username>\\anaconda3\\lib',
 'C:\\Users\\<my username>\\anaconda3',
 'C:\\Users\\<my username>\\anaconda3\\lib\\site-packages',
 'C:\\Users\\<my username>\\anaconda3\\lib\\site-packages\\win32',
 'C:\\Users\\<my username>\\anaconda3\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\<my username>\\anaconda3\\lib\\site-packages\\Pythonwin']

What I’ve tried so far:


import import_ipynb
import self_archiver as sa

myArchive = sa.self_archiver()
AttributeError                            Traceback (most recent call last)
Input In [17], in <cell line: 4>()
      1 import import_ipynb
      2 import self_archiver as sa
----> 4 myArchive = sa.self_archiver()

AttributeError: module 'self_archiver' has no attribute 'self_archiver'

This takes several seconds to produce the error, and notice how the last line of error output shows it is searching for, not self_archiver.ipynb.

import ipynbname
myPath = str(ipynbname.path()) + "self_archiver.ipynb"
%run ./self_archiver.ipynb

myArchive = self_archiver()
OSError                                   Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\IPython\core\magics\, in, parameter_s, runner, file_finder)
    695     fpath = arg_lst[0]
--> 696     filename = file_finder(fpath)
    697 except IndexError as e:

File ~\anaconda3\lib\site-packages\IPython\utils\, in get_py_filename(name)
     90 else:
---> 91     raise IOError('File `%r` not found.' % name)

OSError: File `'./'` not found.

The above exception was the direct cause of the following exception:

Exception                                 Traceback (most recent call last)
Input In [24], in <cell line: 3>()
      1 import ipynbname
      2 myPath = str(ipynbname.path()) + "self_archiver.ipynb"
----> 3 get_ipython().run_line_magic('run', './self_archiver.ipynb')
      5 myArchive = self_archiver()

File ~\anaconda3\lib\site-packages\IPython\core\, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2292     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2293 with self.builtin_trap:
-> 2294     result = fn(*args, **kwargs)
   2295 return result

File ~\anaconda3\lib\site-packages\IPython\core\magics\, in, parameter_s, runner, file_finder)
    705     if == 'nt' and re.match(r"^'.*'$",fpath):
    706         warn('For Windows, use double quotes to wrap a filename: %run "mypath\\"')
--> 707     raise Exception(msg) from e
    708 except TypeError:
    709     if fpath in sys.meta_path:

Exception: File `'./'` not found.


Here I tried making a folder called self_archiver below the one containing A.ipynb, adding self_archiver.ipynb to it, adding an empty per some googling I did into the folder, then running:

from importnb import Notebook
with Notebook():
    from self_archiver import self_archiver

zzz = self_archiver()

TypeError                                 Traceback (most recent call last)
Input In [27], in <cell line: 1>()
----> 1 zzz = self_archiver()

TypeError: 'module' object is not callable

So… I’m stumped. Any help will be appreciated, and I’m happy to try things you suggest.

importnb is a strange and terrible beast. Can you post a reproducer issue?

Ideally, also, would be to demonstrate it as a binder-reproducible gist, ideally with just a requirements.txt like:

1 Like

Starting small with regards to the %run attempts…

You should be able to run notebooks from inside another notebook using %run.
Does using %run to run an external notebook that is in the same directory as the notebook you are currently running work for you normally? I see a lot of red flags in your code and notes in the error report you aren’t paying attention to, but maybe you have %run working in other context so I wanted to check before a I posted suggestions based on that and things I cannot test myself. Sadly I cannot test because I use Unix and not Windows.