在jupyter notebook中写Python代码,发现代码太长了,各种函数和包的定义.
因此为了让代码看着清爽点,可以将其中的一些部分放在桶目录的新建 .py 文件里,
通过 %run xx.py调用该文件,同时为了使用主文件的变量,增加 -i,即 %run -i xx.py。
这样让整个代码变得清爽。
%run xx.py ##需要重新引入包,重新定义变量
%run -i xx.py ##共享包,变量定义
·
翻译/English
I found my jupyter nootbook has a long Python code. I found we could seperate some code block into other python file and use %run command to use them. We could also choose if we want the code in these files share some varities and imported packages.
·
Built-in magic commands
How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook?