How to convert ipynb to py

Mateen KianiMateen Kiani
4 min read

How to convert ipynb to py

Converting Jupyter notebooks into Python scripts can feel like unlocking a new level in your workflow. We all love the mix of code, visualizations, and narrative that .ipynb files offer—but have you ever paused to think about the hidden clutter those notebook-specific commands leave behind? How do you turn a polished notebook into a clean, shareable .py file without manual copy-paste or broken cells?

Luckily, tools like nbconvert and simple Python APIs handle this for you. By automating the conversion from .ipynb to .py, you save time, maintain version control, and prevent quirks from sneaking into your production scripts. Let’s explore how this process works and how you can integrate it into your everyday development.

Why Convert Notebooks

Notebooks excel at exploration and presentation, but they’re not ideal for production. Scripts make collaboration, testing, and deployment smoother. When you convert to .py, you:

  • Create modular code ready for unit tests.
  • Avoid hidden state between cells.
  • Integrate with CI pipelines and code reviews.

Tip: Treat notebooks as drafts, then export polished versions for reuse.

Cleaning notebook metadata is often overlooked. Without conversion, you risk committing bulky JSON files with outputs, execution counts, and sensitive data. Converting ensures only the essential code flows through.

Install and Setup

Before converting, ensure you have Jupyter installed:

pip install jupyter

If Jupyter isn’t recognized, you may need to add Python to PATH. For isolated environments, remember to activate python virtual environments before installing.

Next, confirm nbconvert is available:

jupyter nbconvert --version

If you see a version number, you’re ready. Otherwise, reinstall Jupyter or update your path settings.

Command-Line Conversion

The fastest way to convert is via command line. From your project folder, run:

jupyter nbconvert --to script analysis.ipynb

This creates analysis.py next to the notebook. By default, it:

  1. Strips output cells.
  2. Prepends # In[...] markers to show cell boundaries.
  3. Leaves comments explaining markdown sections.

You can batch-convert all notebooks:

jupyter nbconvert --to script *.ipynb

Pro Tip: Use --output-dir=src to direct all scripts into one folder.

Programmatic Conversion

For more control, use Python’s API:

from nbconvert import PythonExporter
import nbformat

# Load notebook
with open('analysis.ipynb') as f:
    nb = nbformat.read(f, as_version=4)

# Export to Python
exporter = PythonExporter()
body, _ = exporter.from_notebook_node(nb)

# Write to file
with open('analysis.py', 'w') as f:
    f.write(body)

This approach lets you:

  • Strip or preserve comments dynamically.
  • Insert custom headers or licenses.
  • Integrate conversion in your build scripts.

Best Practices for Scripts

Once you have a .py file, tidy it up:

  • Remove cell markers: Search for # In[ and delete if not needed.
  • Refactor repeated code into functions or modules.
  • Use logging instead of print statements for clarity.
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Starting data load")

Keep notebooks for exploration, and scripts for maintenance.

Automating in Workflows

In CI/CD pipelines, conversion can be part of your test suite. For example, in GitHub Actions:

- name: Convert notebooks
  run: |
    pip install jupyter
    jupyter nbconvert --to script notebooks/*.ipynb

Then, you can lint and test resulting scripts:

flake8 src/
pytest tests/

Automation ensures consistency across the team and prevents notebooks from drifting from production code.

Handling Custom Magics

Jupyter magics like %matplotlib inline or %%bash don’t translate directly. You can:

  • Remove them manually after conversion.
  • Pre-process the notebook to strip magics with a script.

Example pre-processor:

import re

cleaned = re.sub(r'^%.*$', '', body, flags=re.MULTILINE)

Or switch %matplotlib inline to standard imports:

import matplotlib.pyplot as plt
plt.show()

Automate magic stripping to keep your scripts clean.

Conclusion

Converting .ipynb files to .py scripts bridges the gap between interactive exploration and production-ready code. With tools like nbconvert and simple Python APIs, you can turn notebooks into clean, modular scripts that integrate with version control, CI pipelines, and team workflows. Automating this process reduces manual errors, ensures consistent code style, and helps you maintain a clear separation between experimentation and deployment. Start integrating conversion into your next project, and you'll find your development flow more reliable and your collaborators happier.

0
Subscribe to my newsletter

Read articles from Mateen Kiani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mateen Kiani
Mateen Kiani