Skip to content

running_external_code

While notebooks are great for interactive development, you often need to run code stored in separate Python files (.py). IPython provides magic commands to execute external scripts or load their content directly into your session.

The %run magic command executes a Python script file. Crucially, the script runs within the same namespace as your IPython session. This means any variables, functions, or classes defined in the script become available in your interactive session after the script finishes.

First, let’s create a simple Python script named utils.py:

%%writefile utils.py
SCRIPT_MESSAGE = "This message comes from utils.py"
def helper_function(x):
print(f"Helper function called with: {x}")
return x * x
print("Executing utils.py...")
result_from_script = helper_function(5)
print("utils.py finished.")

Now, let’s run this script from an IPython cell:

In [1]: %run utils.py
# Output:
# Executing utils.py...
# Helper function called with: 5
# utils.py finished.

After running the script, the variables and functions defined within it are accessible in the current session:

In [2]: print(SCRIPT_MESSAGE)
# Output: This message comes from utils.py
In [3]: print(result_from_script)
# Output: 25
In [4]: helper_function(10)
# Output:
# Helper function called with: 10
Out[4]: 100

You can pass arguments to the script just like on the command line:

# Create script `arg_script.py`
%%writefile arg_script.py
import sys
print(f"Script name: {sys.argv[0]}")
print(f"Arguments: {sys.argv[1:]}")
# Run with arguments
%run arg_script.py arg1 100 --option

The %run magic also has options, like -i, which runs the script and then drops you into an IPython prompt within the script’s namespace (useful for debugging).

Instead of executing a script directly, you might want to bring its code into a notebook cell for inspection or modification before running. The %load magic command does exactly this.

# Cell 1:
%load utils.py

After executing the cell above, its content will be replaced by the content of utils.py, commented out with the original %load command at the top:

# Cell 1 (after executing %load):
# %load utils.py
SCRIPT_MESSAGE = "This message comes from utils.py"
def helper_function(x):
print(f"Helper function called with: {x}")
return x * x
print("Executing utils.py...")
result_from_script = helper_function(5)
print("utils.py finished.")

Now you can edit this code directly in the cell and execute it (e.g., using Shift+Enter) as if you had typed it yourself.

%load can also load code from URLs or specific lines from history (%load -r 1-5 loads lines 1 to 5 from input history).

  • %run: Executes the script immediately in the current namespace. Good for running utility scripts or main application entry points.
  • %load: Inserts the script’s content into a cell for later editing/execution. Good for bringing in examples or code snippets you want to modify.

Note: The older %edit magic command, which opened an external editor, is generally less used now compared to editing files directly in JupyterLab’s editor or using %load followed by cell editing.