Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 998
MichaelRSF Avatar asked Aug 10 '17 02:08

MichaelRSF


People also ask

How do you create and import modules in Jupyter notebook?

Create your own module In order to create a module in Jupyter Lab, first create a new notebook. Rename the notebook (e.g. 'module1. ipynb') and copy paste the code in the notebook. Click 'File', 'Download as' and 'Python'

How do I import a Python module into a Jupyter notebook?

No more typing “import pandas as pd” 10 times a dayCreate a folder called startup if it's not already there. Add a new Python file called start.py. Put your favorite imports in this file. Launch IPython or a Jupyter Notebook and your favorite libraries will be automatically loaded every time!


2 Answers

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

like image 181
ss301 Avatar answered Oct 22 '22 15:10

ss301


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!

like image 40
Muhammad Rizwan Avatar answered Oct 22 '22 16:10

Muhammad Rizwan