Converting Jupyter Notebooks to Python Files: A Quick Guide
Jupyter Notebooks have become an indispensable tool for data scientists and researchers worldwide. They offer an interactive environment where you can mix code, visualizations, and explanatory text, making it easy to create and share data-driven stories.

However, there are times when you might want to convert your Jupyter Notebook into a standalone Python script. This can be useful for version control, sharing with colleagues who don’t use Jupyter, or integration into larger projects. In this guide, we’ll walk you through the steps to perform this conversion.
The nbconvert tool, accessed through the command jupyter nbconvert, facilitates the transformation of notebooks into diverse formats utilizing Jinja templates. This versatile tool empowers you to convert a .ipynb notebook file into static formats such as:
- HTML
- LaTeX
- Reveal JS
- Markdown (md)
- ReStructured Text (rst)
- Executable script
Step 1: Installing
Installing nbconvert
Before we begin, you’ll need to ensure you have the nbconvert tool installed. If you haven't done so already, you can install it via pip:
pip install nbconvert
# OR
conda install nbconvertInstalling Pandoc
For converting markdown to formats other than HTML, nbconvert uses Pandoc (1.12.1 or later).
To install pandoc on Linux, you can generally use your package manager:
sudo apt-get install pandocInstalling TeX
For converting notebooks to PDF (with --to pdf), nbconvert makes use of LaTeX and the XeTeX as the rendering engine.
- Linux: TeX Live; E.g. on Debian or Ubuntu:
sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic- macOS (OS X): MacTeX.
- Windows: Latex Project.
Because nbconvert depends on packages and fonts included in standard TeX distributions, if you do not have a complete installation, you may not be able to use nbconvert’s standard tooling to convert notebooks to PDF.
Step 2: Converting the Notebook
Once you have nbconvert installed, navigate to the directory containing your Jupyter Notebook file using your command prompt or terminal.
To convert the notebook, you’ll use the following command:
# convert to .py
jupyter nbconvert --to script your_notebook.ipynb
# convert to .pdf
jupyter nbconvert --to pdf your_notebook.ipynbReplace your_notebook.ipynb with the actual name of your Jupyter Notebook file. This command will generate a Python script with the same name but a .py extension.
Step 3: Understanding the Result
The generated Python script will contain all the code cells from your notebook. However, it won’t include any markdown cells or output. If you want to retain those elements, you’ll need to manually copy them into the Python file.
Conclusion
Converting a Jupyter Notebook to a Python script is a straightforward process that can enhance the versatility and portability of your code. Whether you’re collaborating on a project, incorporating your code into a larger application, or simply prefer working in a traditional Python environment, this guide provides a quick and easy solution.
By leveraging the power of nbconvert, you can seamlessly transition between the interactive world of Jupyter and the structured environment of Python scripts.
In Plain English
Thank you for being a part of our community! Before you go:
- Be sure to clap and follow the writer! 👏
- You can find even more content at PlainEnglish.io 🚀
- Sign up for our free weekly newsletter. 🗞️
- Follow us: Twitter(X), LinkedIn, YouTube, Discord.
- Check out our other platforms: Stackademic, CoFeed, Venture.






