From the notebook menu you can save the file directly as a python script. Go to the 'File' option of the menu, then select 'Download as' and there you would see a 'Python (.py)' option.
Another option would be to use nbconvert from the command line:
In short: This command-line option converts mynotebook.ipynb to python code:
jupyter nbconvert mynotebook.ipynb --to python
note: this is different from above answer. ipython has been renamed to jupyter. the old executable name (ipython) is deprecated.
More details:jupyter command-line has an nbconvert argument which helps convert notebook files (*.ipynb) to various other formats.
You could even convert it to any one of these formats using the same command but different --to option:
asciidoc
custom
html
latex. (Awesome if you want to paste code in conference/journal papers).
markdown
notebook
pdf
python
rst
script
slides. (Whooh! Convert to slides for easy presentation 😊)
the same command jupyter nbconvert --to latex mynotebook.ipynb
For more see jupyter nbconvert --help. There are extensive options to this. You could even to execute the code first before converting, different log-level options etc.
You definitely can achieve that with nbconvert using the following command:
jupyter nbconvert --to python while.ipynb
However, having used it personally I would advise against it for several reasons:
It's one thing to be able to convert to simple Python code and another to have all the right abstractions, classes access and methods set up. If the whole point of you converting your notebook code to Python is getting to a state where your code and notebooks are maintainable for the long run, then nbconvert alone will not suffice. The only way to do that is by manually going through the codebase.
nbconvert still mixes execution code and library code.
Comments are still not present (probably were not in the notebook).
There is still a lack of unit tests etc.
So to summarize, there is not good way to out of the box convert python notebooks to maintainable, robust python modularized code, the only way is to manually do surgery.
Jupytext allows for such a conversion on the command line, and importantly you can go back again from the script to a notebook (even an executed notebook). See here.
import json
files = ["my_first_file_name.ipynb", "my_second_file_name.ipynb"]
for file in files:
code = json.load(open(file))
py_file = open(f"{file}.py", "w+")
for cell in code['cells']:
if cell['cell_type'] == 'code':
for line in cell['source']:
py_file.write(line)
py_file.write("\n")
elif cell['cell_type'] == 'markdown':
py_file.write("\n")
for line in cell['source']:
if line and line[0] == "#":
py_file.write(line)
py_file.write("\n")
py_file.close()
If this is a one-off, follow e.g. @kikocorreoso depending if you want to use command line or gui.
However, if you want some solution that will maintain a synchronized version of the .py and the .ipynb you really should consider using jupytext as also pointed out by @Wayne
Run conda install jupytext or pip install jupytext
Then do:
jupytext --set-formats ipynb,py <file>.ipynb
This will make sure jupyter keeps the two files in sync when saving from now on...
Last note: If you are a gui person, after running the installation command for jupytext, everything else can be done from the gui as well File-->Jupytext-->pair Notebook with light Script: