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

MichaelRSF
  • 886
  • 5
  • 16
  • 40
  • You can save the file to your computer by going to `File->Download->Python (.py)` – Cory Madden Aug 10 '17 at 02:51
  • 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! – MichaelRSF Aug 10 '17 at 03:07
  • 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... – Cory Madden Aug 10 '17 at 03:15
  • 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 say`Open 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. – MichaelRSF Aug 10 '17 at 04:03
  • Perfect! I'm using Chrome, but the steps are pretty similar and it works. Thanks for sharing. – Cory Madden Aug 10 '17 at 04:07

3 Answers3

11
%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.

ss301
  • 514
  • 9
  • 22
  • 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. – mins Feb 15 '21 at 17:27
  • No need to import again. Just %run line is enough. That'll run that nb as part of your current nb. – ss301 Feb 16 '21 at 18:14
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!

Muhammad Rizwan
  • 184
  • 2
  • 4
  • 1
    I am trying this and it does not work for me. It always says "ModuleNotFoundError:No module named ''" . This occurs when i use it in Azure Databricks python environment – HEART94 Feb 06 '20 at 23:39
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
AJ AJ
  • 187
  • 2
  • 12