๐ŸŒ
Jupyter Notebook
jupyter-notebook.readthedocs.io โ€บ en โ€บ stable โ€บ examples โ€บ Notebook โ€บ Importing Notebooks.html
Importing Jupyter Notebooks as Modules โ€” Jupyter Notebook 7.6.3 documentation
If all of your notebook cells are pure-Python, this step is unnecessary. ... class NotebookLoader(object): """Module Loader for Jupyter Notebooks""" def __init__(self, path=None): self.shell = InteractiveShell.instance() self.path = path def load_module(self, fullname): """import a notebook as a module""" path = find_notebook(fullname, self.path) print("importing Jupyter notebook from %s" % path) # load the notebook object with io.open(path, 'r', encoding='utf-8') as f: nb = read(f, 4) # create the module and add it to sys.modules # if name in sys.modules: # return sys.modules[name] mod = type
Discussions

python - Jupyter - importing class from an imported notebook - Stack Overflow
I'm trying to import a class from another notebook and following this tutorial I am doing something wrong which i don't get. I have #(Building.ipynb) class Class (): def __init__ (): ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 5, 2017
How can I import python custom class in Anaconda (Jupyter notebook) - Stack Overflow
I don't manage to find how import custom class in Python with the Jupyter notebook in anaconda. In my work folder I have a file 'user.ipynb' that contains a class name User. In an other file in the same folder, I try to import this class with : from user import User. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Trying to use a class in one Jupyter notebook in another; nothing I've tried works
How about putting the class to a .py file and importing that in both notebooks? More on reddit.com
๐ŸŒ r/learnpython
9
1
October 18, 2022
How to import a python class/module in a jupyter notebook? - Stack Overflow
Copyfrom src.main import DoSomething ... when i run this notebook , i get Module not found error : No module named 'src.main': 'src' is not a package. Although, i knew this wouldn't work but i tried to move main.py to the root of my project and tried ... Save this answer. ... Show activity on this post. add an empty __init__.py file ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-import-python-file-as-module-in-jupyter-notebook
How to Import Python File as Module in Jupyter Notebook | Saturn Cloud Blog
May 1, 2026 - For example, to import the my_module.py file we created earlier, we would use the following code in our Jupyter notebook: ... After importing the Python file as a module, you can then use any function, class, or variable defined in the file in your notebook.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ trying to use a class in one jupyter notebook in another; nothing i've tried works
r/learnpython on Reddit: Trying to use a class in one Jupyter notebook in another; nothing I've tried works
October 18, 2022 -

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 callable

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

Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ pysnippet
import python class in jupyter notebook - YouTube
Instantly Download or Run the code at https://codegive.com title: importing python classes in jupyter notebook: a step-by-step tutorialintroduction:jupyter ...
Published: February 29, 2024
Views: 92
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61281064 โ€บ how-can-i-write-a-python-class-in-a-jupyter-notebook-and-then-import-it-to-use-a
How can I write a python class in a jupyter notebook and then import it to use again? - Stack Overflow
I have written a class that has several methods that do some basic calculations. I've written it in jupyter notebook, and save it as a BaseModel.ipynb file. the code all works when i use it in the same notebook. i want to be able to import the class when working in a fresh jupyter notebook to keep things less cluttered as the class is fairly long, and thought i would be able to save it as an .ipynb or .py and then in new notebook do import BaseModel but then i get the error
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ importnb
importnb imports notebooks as python modules.
the importnb command line interface mimics python's. it permits running notebooks files, modules, and raw json data. the commands below execute a notebook module and file respectively. importnb -m Untitled # call the Untitled module as __main__ ...
      ยป pip install importnb
    
Published: Nov 02, 2023
Version: 2023.11.1
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 49295824 โ€บ how-to-import-my-own-class-that-uses-an-imported-module-into-jupyter-notebook
python - How to import my own class that uses an imported module into jupyter notebook - Stack Overflow
March 15, 2018 - In the same directory, I have a Jupyter notebook where I want to use this class. In this notebook, I have: import numpy as np import sys sys.path.append('path/to/file') import recording #or from recording import Recording r = recording.Recording(initialization arguments) r.method_that_uses_numpy()
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to import Python modules in Jupyter Notebook - YouTube
Here is the outline of the whole video-0:00 : Create a new folder greet_folder 0:12 : Open this folder in jupyter notebook0:20 : Create new Python notebook i...
Published: December 17, 2020
๐ŸŒ
Jupyter Community Forum
discourse.jupyter.org โ€บ meta
How to import a .py file from a directory - Meta - Jupyter Community Forum
March 20, 2021 - I am trying to import a .py file into my jupyter notebook. using the following code , but it is throwing an error. I have also included init.py file in the directory import sys import os sys.path.append(os.path.abspath("/Users/hp/Documents/AFR")) import test_standard.py output No module named ...
๐ŸŒ
QuantConnect
quantconnect.com โ€บ forum โ€บ discussion โ€บ 16816 โ€บ need-help-to-import-a-class-from-another-file
Need help to import a class from another file - QuantConnect.com
April 7, 2024 - Python Version: Ensure your Python environment is correctly set up and that you're using a version compatible with your code. If you're working in a Jupyter notebook and want to import classes from another Python file, the process is similar, but make sure the notebook's kernel is running in the environment where the Python files are accessible, and the files are in the notebook's working directory or in a directory that's in the Python path.
๐ŸŒ
Jupyter Notebook
jupyter-notebook.readthedocs.io โ€บ en โ€บ 4.x โ€บ examples โ€บ Notebook โ€บ rstversions โ€บ Importing Notebooks.html
Importing Jupyter Notebooks as Modules โ€” Jupyter Notebook 4.4.1 documentation
If all of your notebook cells are pure-Python, this step is unnecessary. class NotebookLoader(object): """Module Loader for Jupyter Notebooks""" def __init__(self, path=None): self.shell = InteractiveShell.instance() self.path = path def load_module(self, fullname): """import a notebook as a module""" path = find_notebook(fullname, self.path) print ("importing Jupyter notebook from %s" % path) # load the notebook object with io.open(path, 'r', encoding='utf-8') as f: nb = current.read(f, 'json') # create the module and add it to sys.modules # if name in sys.modules: # return sys.modules[name]
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python module creation and importing in Jupyter notebook - YouTube
In JupyterLab, you can import user-created modules by exporting the Notebook. This will download the .py file to your computer. The downloaded file can be im...
Published: September 6, 2022
๐ŸŒ
Medium
skaf.medium.com โ€บ import-python-files-into-jupyter-notebook-while-being-in-another-directory-393e1bf76637
Import Python files into Jupyter Notebook while being in another directory.
December 9, 2023 - This code will add the path specified in this statement os.path.abspath(os.path.join(โ€˜..โ€™)) to the system PATH variable, allowing you to importโ€ฆ ... AI expert, research scientist, and blogger pursuing a Ph.D. in Artificial Intelligence I write about Data Science, AI, and Python TIPS & TRICKS.
๐ŸŒ
Tanso Biosciences
tansobio.com โ€บ home โ€บ blog โ€บ how to import a jupyter notebook like a python module
How to import a Jupyter notebook like a Python module
June 1, 2021 - By editing the config file jupyter_notebook_config.py, you can get Jupyter to virtually import a notebook like a Python module.