简体   繁体   中英

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

%run ./module_code.ipynb

将此保留在导入部分中-将 module_code 替换为您的文件名,然后您可以从新笔记本访问该文件中的函数。

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!

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 import and use that module in other notebooks.

import example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM