IPython
Jupyter Notebook
Python
Command Line
Conversion

How do I convert a IPython Notebook into a Python file via commandline?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The normal command-line way to convert a notebook into a Python script is to use Jupyter's nbconvert tool. That gives you a .py file you can commit, lint, or run outside the notebook UI while preserving the code cells in normal script form.

Use jupyter nbconvert

From the folder containing the notebook, run:

bash
jupyter nbconvert --to script notebook.ipynb

For a Python notebook, that creates a .py file next to the original notebook.

If you want to force the output name:

bash
jupyter nbconvert --to script notebook.ipynb --output my_script

That usually produces my_script.py.

This is the standard answer because nbconvert is part of the Jupyter toolchain and is designed for exactly this kind of format conversion.

Install the Tool if Needed

If the command is missing, install Jupyter or nbconvert in the environment that owns your notebook tooling:

bash
python -m pip install jupyter

Then verify:

bash
jupyter --version

Using python -m pip is safer than relying on a random pip executable, because it ensures the installation goes into the interpreter you actually plan to use.

What the Conversion Looks Like

The generated script usually contains:

  • notebook code cells as normal Python code
  • markdown cells turned into comments
  • cell markers or comments showing notebook boundaries

That means the script is readable, but not always production-ready immediately. A notebook often contains exploratory steps, repeated imports, or out-of-order execution assumptions that are fine in an interactive environment and awkward in a clean script.

A Simple Example

Suppose demo.ipynb contains:

  • one markdown explanation cell
  • one code cell that prints a value

After:

bash
jupyter nbconvert --to script demo.ipynb

you will get something conceptually similar to:

python
1# Notebook explanation
2
3value = 42
4print(value)

That is exactly why nbconvert is useful for moving from exploratory work toward script-based workflows.

Watch for Notebook-Specific Magics

Notebook code sometimes contains IPython magics such as:

  • '%matplotlib inline'
  • '%timeit'
  • '!pip install ...'

Those are convenient in notebooks but not always valid in a plain Python script. After conversion, you may need to replace them with standard Python or shell equivalents.

For example:

python
1import subprocess
2import sys
3
4subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])

or, more commonly, remove that installation step from the script entirely and manage dependencies outside the runtime.

Use the Command in Automation Carefully

You can run nbconvert in CI or build scripts, but do it intentionally. Conversion is useful when:

  • you want a generated script artifact
  • you want to inspect notebook code in text form
  • you need downstream tooling that expects .py

It is less useful if the notebook is still highly interactive and not meant to behave like a linear script.

In other words, conversion is easy, but successful script use still depends on whether the notebook content makes sense outside the notebook execution model.

Common Pitfalls

  • Using jupyter nbconvert successfully and then assuming the output script is automatically clean production code.
  • Forgetting that notebook magics and shell escapes may need manual cleanup after conversion.
  • Installing Jupyter into one Python environment and trying to run jupyter from another.
  • Expecting notebook cell execution order quirks to disappear automatically in the script.
  • Converting the notebook without checking where the output file was written or what it was named.

Summary

  • Use jupyter nbconvert --to script notebook.ipynb to convert a notebook to a Python file from the command line.
  • Install Jupyter if the jupyter command is not available.
  • The output script usually contains code cells as Python and markdown as comments.
  • Review the generated file for notebook-only magics and exploratory code patterns.
  • Conversion is the easy part; making the script clean and linear is still your responsibility.

Course illustration
Course illustration

All Rights Reserved.