How to save python script as .py file on jupyter notebook

对着背影说爱祢 提交于 2020-07-04 13:57:51

问题


I wrote some functions on Jupyter notebook and want to save the script in form of .py, but anaconda jupyter notebook automatically saves the script as .ipynb, How can I change it to .py for this specific script? Is there any command for it? Thanks


回答1:


There are built-in Magic commands in Jupyter:

IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

Magics are useful as convenient functions where Python syntax is not the most natural one, or when one want to embed invalid python syntax in their work flow.

%%writefile (Write the contents of the cell to a file)

%save (Save a set of lines or a macro to a given filename)




回答2:


You can download a copy of the python as a .py script from FILE menu in the upper left corner. See this photograph.

That should do it!




回答3:


you can try this in command line

ipython nbconvert --to python mnist.py.ipynb.

to convert all

 ipython nbconvert --to python *.ipynb


来源:https://stackoverflow.com/questions/52885901/how-to-save-python-script-as-py-file-on-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!