How to create modules in Jupyter notebook and import them? Python
Asked Answered
C

3

16

I've created multiple python modules as .py files in a Python IDE called Pyzo in the following path: 'C:\Users\Michael\Anaconda3\Lib\site-packages' which I can then import like regular Python packages such as pandas and numpy into my Jupyter notebook or into Pyzo.

I'm a bit lost as to how to create a module in Jupyter notebook, containing a class with say a simple function, which I can then save and import into a new Jupyter notebook file.

The examples in this link below I found extremely vague and overly complicated. Any simpler examples would help, thanks! http://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Importing%20Notebooks.ipynb

Cohl answered 10/8, 2017 at 2:43 Comment(5)
You can save the file to your computer by going to File->Download->Python (.py)Sawtelle
Hey Cory, thanks for the response. It more a less works. When I save a Jupyter notebook as a .py file, it's sent to my Download folder. I'm unable to directly save it in the required location of C:\Users\Michael\Anaconda5\Lib where I have all my Python modules saved, and can then import from this folder. So I have to copy the downloaded .py file from the Download folder, and paste into the Anaconda5\Lib folder. Other than that, it's pretty much what I needed, way more simpler than the link I was suggested. Thanks!Cohl
No problem. I didn't know if it was possible, either. Good to know that utility is available. I agree that the automatic download to your download folder is rather lame. My download folder is getting pretty cluttered...Sawtelle
I've sort of gotten round that issue. I'm using Firefox and logged into my gmail account. If you have the same, go to the top right corner of any firefox window, and you'll see the symbol of 3 horizontal lines which is below the delete x for any window, if you hover over it, it'll sayOpen Menu. Click on it, select Options. A new window will appear titled General. On the Download section, choose Always ask where to save files. Now, save any file .py in Jupyter notebook as usual. Once you click OK, a new window will appear, allowing to set a specific download location as default.Cohl
Perfect! I'm using Chrome, but the steps are pretty similar and it works. Thanks for sharing.Sawtelle
S
12
%run ./module_code.ipynb

keep this in import section- replace module_code with your file name and then you can access functions inside that file from the new notebook.

Seaboard answered 20/3, 2019 at 7:22 Comment(2)
What does mean "keep this in import section"? I have a nb named managers in the same directory than the current nb. This nb contains several classes. I add %run ./managers.ipynb in the current nb, then import managers and get ModuleNotFoundError: No module named 'managers'. Can you clarify your solution. Tks.Bloodline
No need to import again. Just %run line is enough. That'll run that nb as part of your current nb.Seaboard
B
7

Suppose you want to import the contents of A.ipynb into B.ipynb.

Installation

pip install import-ipynb

How to use Place both ipynb files in the same directory. Then, in the B.ipynb:

import import_ipynb
import A

Congratulations! You can now run any functions defined in A.ipynb from B.ipynb!

Babur answered 8/5, 2019 at 16:59 Comment(1)
I am trying this and it does not work for me. It always says "ModuleNotFoundError:No module named '<notebook_name>'" . This occurs when i use it in Azure Databricks python environmentHarmon
S
0

The easiest way to import user created modules on JupyterLab is to Export Notebook as an executable script from File menu as in screenshot. This will download .py file to your pc or mac. Just drag that downloaded file to your jupyterlab to import.

download notebook as .py file

Now you can upload in the same .ipynb directory, and use that module in other notebooks.

import example
Sciurine answered 1/10, 2021 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.