Have you tried loading the python file in the notebook cell?
%load pyfile.py
Answer from dEBA M on Stack OverflowHave you tried loading the python file in the notebook cell?
%load pyfile.py
When you do import pyfile only the module is imported and not other names inside the module if you need the Data_dict class then you need to import it as well. Also, you can use names inside the module with module_name.anything.
In your case, you can do something like this.
Import everything from inside pyfile
from pyfile import *
d = Data_dict()
Only import the Data_dict class
from pyfile import Data_dict
d = Data_dict()
Import the module and then access members through it
import pyfile
d = pyfile.Data_dict()
python - Jupyter - importing class from an imported notebook - Stack Overflow
How can I import python custom class in Anaconda (Jupyter notebook) - Stack Overflow
Trying to use a class in one Jupyter notebook in another; nothing I've tried works
How to import a python class/module in a jupyter notebook? - Stack Overflow
Hi,
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):
Javascript('IPython.notebook.save_notebook()')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\\python39.zip', '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_ipynb:
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'
--------------------------------------------------
Magic This takes several seconds to produce the error, and notice how the last line of error output shows it is searching for self_archiver.py, 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\execution.py:696, in ExecutionMagics.run(self, 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\path.py:91, in get_py_filename(name)
90 else:
---> 91 raise IOError('File `%r` not found.' % name)
OSError: File `'./self_archiver.ipynb.py'` 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\interactiveshell.py:2294, 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\execution.py:707, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
705 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
706 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
--> 707 raise Exception(msg) from e
708 except TypeError:
709 if fpath in sys.meta_path:
Exception: File `'./self_archiver.ipynb.py'` not found.importnb:
Here I tried making a folder called self_archiver below the one containing A.ipynb, adding self_archiver.ipynb to it, adding an empty __init__.py 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 callableSo... I'm stumped. Any help will be appreciated, and I'm happy to try things you suggest.
ยป pip install importnb
There is no simple way to import python files in another directory. This is unrelated to the jupyter notebook
Here are 3 solutions to your problem
- You can add the directory containing the file you want to import to your path and then import the file like this:
import sys
sys.path.insert(1, '/path/to/application/app/folder')
import file
You can create a local module by having an empty
__init__.pyfile in the folder you want to import. There are some weird rules regarding the folder hierarchy that you have to take into consideration.You can create a module for the file you wish to import and install it globally.

Scenario: You have a folder named Jupyter containing your notebook, and you want to import a Python module (e.g., employee.py) located in a sibling folder named nn_webserver.
Folder structure
project_root
โโ Jupyter
โ โโ my_notebook.ipynb <- You're here
โโ nn_webserver
โโ employee.py <- Wish to import from here
Code
import os
import sys
# Build an absolute path from this notebook's parent directory
module_path = os.path.abspath(os.path.join('..', 'nn_webserver'))
# Add to sys.path if not already present
if module_path not in sys.path:
sys.path.append(module_path)
# Now you can import the desired function or class
from employee import motivation_to_work
Read more about imports here and here. Also explore linked question.