IPython Cell Executor
A VS Code extension that allows you to execute Python cells in an IPython terminal with shared namespace between cells.
Features
- Execute Python cells (delimited by
#%%) in an IPython terminal
- Shared namespace between cells and the IPython terminal (variables and imports persist)
- Line-by-line execution for selections (perfect for debugging sessions)
- Automatically creates an IPython terminal if none exists
- Returns focus to the editor after execution
- Supports both regular development and debugging workflows
Requirements
- VS Code 1.74.0 or higher
- IPython must be installed in your Python environment
Installation
From the VS Code marketplace (recommended)
This extension is available on the VS Code Marketplace here.
From VSIX file
- Download the
.vsix file
- Open VS Code
- Go to Extensions view (Ctrl+Shift+X)
- Click the "..." menu at the top of the Extensions view
- Select "Install from VSIX..."
- Navigate to and select the
.vsix file
From source
- Clone this repository
- Run
npm install to install dependencies
- Run
npm run compile to build the extension
- Package the extension:
vsce package --allow-missing-repository --skip-license
- Install the resulting
.vsix file in VS Code using "Install from VSIX..."
Usage
Key bindings
Ctrl+Alt+Enter: Execute the current cell in the IPython terminal
Ctrl+Shift+Enter: Execute the current selection in the IPython terminal (line-by-line)
Commands
You can also use the Command Palette (Ctrl+Shift+P) to run the following commands:
IPython: Execute Current Cell in Terminal
IPython: Execute Current Selection in Terminal
How it works
Cell execution
When you execute a cell:
- The extension identifies the cell containing your cursor (delimited by
#%%)
- It removes the cell delimiter line if present
- It copies the code to the clipboard
- It uses IPython's
%paste -q magic to execute the code in the active namespace
- Focus returns to the editor
Selection execution
When you execute a selection:
- The extension sends each line of your selected code individually to the IPython terminal
- This works even when you're at a PDB or IPDB prompt
- Focus returns to the editor after execution
PDB mode support
When debugging with PDB or IPDB, you can toggle "PDB Mode" to improve how code is executed:
- Use
Ctrl+Alt+, to toggle between regular IPython mode and PDB mode
- In PDB mode, the extension uses specialized line-by-line execution that preserves variable scope and state
- A status bar indicator shows whether you're in "IPython Mode" or "PDB Mode"
Selection execution in PDB mode
When in PDB mode, selection execution (Ctrl+Shift+Enter) has smart behavior:
- For single-line expressions (e.g., variable names), it sends the expression directly to the terminal
- This allows you to see the values of variables by simply selecting and executing them
- For multi-line code or statements with control structures, it uses a specialized line-by-line execution
- This preserves your PDB session state and allows complex code blocks to execute properly
- Variables defined in executed code blocks will be available in your PDB session
Indentation handling
The extension automatically handles indented code blocks:
- When executing code in PDB mode, the extension normalizes indentation before sending it line-by-line
- This means you can select and execute code that's indented within functions or classes
- The extension also handles line continuations (backslash at the end of a line)
- These formatting improvements ensure your code executes correctly within the PDB session
Debugging workflow
- Execute cells with
Ctrl+Alt+Enter during normal development
- When you hit a breakpoint (using
pdb.set_trace(), breakpoint(), etc.), switch to PDB mode with Ctrl+Alt+,
- Use the selection execution method that fits your current need:
- Select a variable name and press
Ctrl+Shift+Enter to see its value
- Select a multi-line code block and press
Ctrl+Shift+Enter to execute it with proper indentation
- When you exit PDB, switch back to regular mode with
Ctrl+Alt+,
- All variables defined during PDB mode execution will be available in your debugging session
Customization
You can customize the extension's keyboard shortcuts through VS Code's keyboard shortcuts editor:
- Open VS Code's keyboard shortcuts (File > Preferences > Keyboard Shortcuts)
- Search for "ipython" to find and modify the extension's commands
Troubleshooting
Terminal not found
If the extension can't find your IPython terminal:
- Open a new terminal in VS Code
- Run
ipython
- Try executing your cell again
Execution not working
Make sure:
- Your cursor is inside a Python cell
- IPython is properly installed in your environment
- Your code doesn't contain syntax errors
Development
Publishing updates to the VS Code Marketplace
To publish a new version of this extension:
Update the version in package.json:
"version": "0.2.8"
Follow semantic versioning: MAJOR.MINOR.PATCH
Compile the extension:
npm run compile
Publish to the marketplace:
vsce publish -p YOUR_PERSONAL_ACCESS_TOKEN
About the Personal Access Token (PAT)
- The PAT is required for every publish operation (not just the initial one)
- Get your PAT from Azure DevOps: https://dev.azure.com
- The PAT must have Marketplace (Manage) scope
Publisher account