如何向IPython Notebook中导入.py文件

来源:互联网 发布:原油软件 编辑:程序博客网 时间:2024/06/07 06:23

IPython Notebook使用起来简洁方便,但是有时候如果需要导入一个现有的.py文件,则需要注意选择导入的方法以达到不同的效果。目前遇到3种方法。


(1) 直接将.py文件拖拽到Notebook界面中的列表里


ipython notebook

(2) 利用 %load:


%load test.py

这种方法可以将自己编写的.py文件原封不动地搬到一个cell里面,自然也往往不是我们想要达到的效果。


(3) 利用 IPython.nbformat.current


import IPython.nbformat.current as nbfnb = nbf.read(open('test.py', 'r'), 'py')nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

这样会生成一个名为 test.ipynb 的文件


运行计时


我们可以通过ExecutionTime插件来对每个cell的运行计时。

pip install jupyter_contrib_nbextensionsjupyter contrib nbextension install --userjupyter nbextension enable execute_time/ExecuteTime

References


向IPython Notebook中导入.py文件

Cannot import .py file to ipython notebook

Converting to (not from) ipython Notebook format

0 0
原创粉丝点击