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

you can try this in command line

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

to convert all

 ipython nbconvert --to python *.ipynb

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

That should do it!


You can easily do this using jupytext. Install jupytext

pip install jupytext

Edit the file below

.jupyter/jupyter_lab_config.py

Add this code snippet to the bottom of your script. This will save to both a py file and a ipynb file when the notebook is saved.

c.NotebookApp.contents_manager_class="jupytext.TextFileContentsManager"
c.ContentsManager.formats = "ipynb,py"

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)