如何在Sublime Text中查看Python脚本变量中间值?能否类MATLAB方式展示?
Hey there! Let's break down how to handle your two Python debugging needs in Sublime Text, since you're used to MATLAB's workflow.
1. 显示Python脚本的变量中间值
You've got a few solid options here, depending on how heavy-duty you need the debugging to be:
Quick-and-dirty print statements:The simplest approach—just drop
print()calls wherever you need to check a variable. For clearer output, use f-strings likeprint(f"Loop iteration {i}: current value of x = {x}"). It's basic, but great for quick checks without setting up extra tools. Just remember to clean them up when you're done!Use the Sublime Debugger plugin:This is the most robust option for proper debugging. Here's how to set it up:
- Install it via Package Control (Ctrl+Shift+P → "Package Control: Install Package" → search for "Sublime Debugger").
- Open your Python script, click in the gutter (the area next to line numbers) to set a breakpoint on the line where you want to inspect variables.
- Press Ctrl+Shift+D to open the debug panel, then select your Python environment to start debugging.
- As you step through code (using the debug controls: step over, step into, etc.), the debug panel will show all variables in the current scope, updating their values in real time.
Anaconda plugin:If you already use Anaconda for code completion, it also includes debugging tools. Set breakpoints, launch the debugger, and you'll see a sidebar panel with all active variables and their current values.
2. 实现类似MATLAB的工作区+命令窗口功能
Sublime Text doesn't have a native "workspace" like MATLAB, but we can replicate the core functionality with plugins and custom setups:
Leverage Sublime Debugger's console:When you're in a debugging session, the debug panel has a console where you can type variable names directly to see their values—just like MATLAB's command window. For example, if you've got a variable
data_framein scope, typedata_frameinto the debug console and hit enter, and it'll print the full value (or a preview for complex objects).Run scripts in IPython interactively:IPython keeps your session alive after running a script, so you can inspect variables afterward. To set this up:
- Go to Tools → Build System → New Build System.
- Paste this JSON config:
{ "cmd": ["ipython", "-i", "$file"], "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"} } - Save it as
Python (IPython).sublime-build. - Select this build system and run your script (Ctrl+B). After the script finishes, you'll be in an IPython prompt where you can type any variable name to see its value, run additional code, or modify variables on the fly.
SublimeREPL for in-editor interactive sessions:This plugin lets you run an interactive Python session right inside Sublime, and send code from your script to it:
- Install SublimeREPL via Package Control.
- Open a Python REPL by going to Tools → SublimeREPL → Python → Python.
- Select the code in your script that you want to run, right-click, and choose "SublimeREPL → Eval in REPL" (or use the keyboard shortcut).
- Once the code runs, you can type variable names directly into the REPL window to view their values, just like you would in MATLAB's command window. The REPL keeps all variables in memory until you close the session.
内容的提问来源于stack exchange,提问作者Balakrishnan Devarajan

