问题
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