You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sublime Text中查看Python脚本变量中间值?能否类MATLAB方式展示?

在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 like print(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:

    1. Install it via Package Control (Ctrl+Shift+P → "Package Control: Install Package" → search for "Sublime Debugger").
    2. 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.
    3. Press Ctrl+Shift+D to open the debug panel, then select your Python environment to start debugging.
    4. 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_frame in scope, type data_frame into 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:

    1. Go to Tools → Build System → New Build System.
    2. Paste this JSON config:
      {
          "cmd": ["ipython", "-i", "$file"],
          "selector": "source.python",
          "env": {"PYTHONIOENCODING": "utf-8"}
      }
      
    3. Save it as Python (IPython).sublime-build.
    4. 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:

    1. Install SublimeREPL via Package Control.
    2. Open a Python REPL by going to Tools → SublimeREPL → Python → Python.
    3. Select the code in your script that you want to run, right-click, and choose "SublimeREPL → Eval in REPL" (or use the keyboard shortcut).
    4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:05:55