IPython
Jupyter Notebook
Python
Text Files
Coding Tutorial

How to load/edit/run/save text files .py into an IPython notebook cell?

Master System Design with Codemia

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

Introduction

In IPython or Jupyter, the normal workflow for a .py file is: load it into a cell, edit the cell, run it, and then write the edited contents back to disk. There is no single built-in “open a file and keep it permanently linked to this cell” feature, so you usually combine a few notebook magics to get the effect.

Load a Python File Into a Cell

Use %load to insert the contents of a file into the current cell:

python
%load my_script.py

After you run that, the cell is replaced with the file contents. In many notebook front ends, IPython also leaves a comment such as # %load my_script.py at the top so you can remember where the code came from.

This is the simplest way to pull an existing script into a notebook for inspection or experimentation.

Edit the Code in the Cell

Once the file is loaded, the code is just notebook cell content. You can change functions, add print statements, comment out blocks, or refactor directly in the editor.

For example, suppose my_script.py originally contains:

python
def greet(name):
    return f"Hello, {name}"

You might load it into a cell and edit it to:

python
1def greet(name):
2    message = f"Hello, {name}"
3    print(message)
4    return message

At this point, the notebook cell and the file on disk are no longer automatically synchronized. Your edits exist only in the notebook until you explicitly write them back out.

Run the Code

There are two common ways to run the code.

If the code is already in the current cell, execute the cell normally with the notebook run command. That defines functions and runs top-level statements exactly like any other code cell.

If you want to run the file from disk as a script, use %run:

python
%run my_script.py

%run is useful when you want to execute the saved file exactly as it exists on disk, including any command-line-style top-level code inside it.

Save the Edited Cell Back to a .py File

To write the contents of a cell back to a file, use %%writefile as the first line of the cell:

python
1%%writefile my_script.py
2def greet(name):
3    message = f"Hello, {name}"
4    print(message)
5    return message

When that cell runs, IPython writes the cell contents to my_script.py.

This is the usual save step after editing code that you loaded earlier with %load.

A Practical Notebook Workflow

A reliable edit cycle looks like this:

  1. load the file with %load my_script.py
  2. edit the cell contents
  3. run the cell to test the changes
  4. save the final version with %%writefile my_script.py
  5. optionally run %run my_script.py to confirm the saved file behaves as expected

That flow makes the boundary between notebook state and file state explicit, which prevents a lot of confusion.

When to Use a Real File Editor Instead

For quick tweaks, the cell-based workflow is fine. For larger scripts, it often becomes awkward because the notebook is not a full source editor. In JupyterLab, opening the .py file in the text editor panel and running it from the notebook can be cleaner.

A common pattern is:

  • keep reusable code in .py modules
  • import or %run those modules from the notebook
  • use the notebook mainly for exploration and experiments

That keeps the script version-controlled and easier to test outside the notebook.

A few notebook commands often pair well with this workflow:

python
1%load my_script.py
2%run my_script.py
3%pwd
4%ls

%pwd helps you confirm the current working directory, which matters when %load says it cannot find the file. %ls lets you check that the file really exists where you think it does.

Common Pitfalls

The biggest pitfall is forgetting that %load copies file contents into the cell once. After that, editing the cell does not change the file automatically.

Another common mistake is mixing up “run the cell” and “run the saved file.” If you edit the cell and then call %run my_script.py without using %%writefile, you are executing the old version on disk, not your edited cell.

Path errors also cause confusion. The notebook kernel has a working directory, and %load my_script.py looks relative to that directory unless you give an absolute path.

Finally, avoid turning large production codebases into giant notebook cells. Notebooks are excellent for exploration, but .py files remain easier to lint, test, diff, and reuse.

Summary

  • Use %load to bring a .py file into a notebook cell.
  • Edit the code directly in the cell.
  • Run the cell normally or use %run to execute the saved file.
  • Use %%writefile to save edited cell contents back to disk.
  • Remember that notebook cells and files are not automatically synchronized.

Course illustration
Course illustration

All Rights Reserved.