To convert a Jupyter Notebook (.ipynb) file to a Python script (.py), you can use the
nbconvert command-line tool provided by Jupyter. Here’s how you can do it:
- Install the
nbconvertpackage if it’s not already installed. You can install it using the following command:
pip install nbconvert
- Open a terminal or command prompt and navigate to the directory where your Jupyter Notebook file is located.
- Run the
nbconvertcommand to convert the notebook to a Python script. Use the
--toflag to specify the output format as a Python script. Here’s the command you can use:
jupyter nbconvert --to script notebook.ipynb
notebook.ipynb with the actual name of your Jupyter Notebook file.
- After running the command, a new Python script file will be created in the same directory as your Jupyter Notebook. The script file will have the same name as the notebook file but with the
The converted Python script will contain the code from the notebook cells, including Markdown and code comments, in a sequential manner.
Please note that the conversion process may depend on the version of Jupyter and
nbconvert you have installed. If you encounter any issues or need more advanced conversion options, you can refer to the
nbconvert documentation for additional options and customization: https://nbconvert.readthedocs.io/