目录
1. Create a Custom Build System
For macOS (using Terminal.app or iTerm2)
For Linux (using gnome-terminal or xterm)
For Windows (using Command Prompt or PowerShell)
2. Use the Build System
Notes
首页 开发工具 sublime 如何从Sublime Text中运行当前的Python文件?

如何从Sublime Text中运行当前的Python文件?

Aug 21, 2025 am 08:41 AM

要让当前Python文件在Sublime Text中通过新终端窗口运行,需创建自定义构建系统:1. 依次选择工具→编译系统→新建编译系统,粘贴对应操作系统的JSON配置;2. 根据操作系统选择命令(macOS使用osascript调用Terminal或iTerm2,Linux使用gnome-terminal等,Windows使用start cmd或PowerShell);3. 保存为Python-Terminal.sublime-build并选中该编译系统;4. 按Ctrl B(或Cmd B)运行,终端将执行脚本并保持打开以便查看输出,最终完成整个执行流程。

How to run the current Python file in a new terminal from Sublime Text?

To run the current Python file in a new terminal from Sublime Text, you need to customize a build system that opens a new terminal window and executes the Python script. Here's how to set it up depending on your operating system.

How to run the current Python file in a new terminal from Sublime Text?

1. Create a Custom Build System

Go to Tools > Build System > New Build System… in Sublime Text. This will open a new file where you can define the build configuration.

For macOS (using Terminal.app or iTerm2)

{
    "cmd": ["osascript", "-e", "tell app \"Terminal\" to do script \"python3 '$file'; read -p 'Press Enter to exit...'\""],
    "selector": "source.python",
    "shell": true,
    "working_dir": "$file_path"
}

If you're using iTerm2 instead of Terminal, replace "Terminal" with "iTerm" and adjust the command accordingly.

How to run the current Python file in a new terminal from Sublime Text?

Example for iTerm2:

{
    "cmd": ["osascript", "-e", "tell app \"iTerm\" to create window with default profile command \"python3 '$file'; read -p 'Press Enter to exit...'\""],
    "selector": "source.python",
    "shell": true,
    "working_dir": "$file_path"
}

Save this as Python-Terminal.sublime-build.

How to run the current Python file in a new terminal from Sublime Text?

For Linux (using gnome-terminal or xterm)

{
    "cmd": ["gnome-terminal", "--", "bash", "-c", "python3 '$file'; read -p 'Press Enter to exit...'"],
    "selector": "source.python",
    "shell": true,
    "working_dir": "$file_path"
}

Replace gnome-terminal with xterm or konsole if you're using a different terminal emulator.

Save this as Python-Terminal.sublime-build.

For Windows (using Command Prompt or PowerShell)

Using Command Prompt:

{
    "cmd": ["start", "cmd", "/k", "python", "$file"],
    "shell": true,
    "working_dir": "$file_path",
    "selector": "source.python"
}

Using PowerShell:

{
    "cmd": ["start", "powershell", "-NoExit", "-Command", "python '$file'"],
    "shell": true,
    "working_dir": "$file_path",
    "selector": "source.python"
}

Save the file appropriately and choose the build system via Tools > Build System.

2. Use the Build System

After saving the .sublime-build file:

  • Select it from Tools > Build System.
  • Press Ctrl B (or Cmd B on macOS) to run the current Python file in a new terminal window.

The script will execute, and the terminal will remain open (thanks to read or /k) so you can see the output.

Notes

  • Make sure python or python3 is in your system’s PATH.
  • The $file variable expands to the full path of the current file.
  • working_dir ensures the script runs in the correct directory.
  • On macOS and Linux, read -p keeps the terminal open until you press Enter.
  • On Windows, /k does the same for cmd.

Basically, just save the right build config for your OS, select it, and use Build (Ctrl B). It’s not hard once set up, but small syntax errors in the JSON or command can break it — double-check the commas and quotes.

以上是如何从Sublime Text中运行当前的Python文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驱动投资研究,做出更明智的决策

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

如何将崇高文本用作git的差异工具? 如何将崇高文本用作git的差异工具? Sep 21, 2025 am 01:06 AM

配置Git使用SublimeText作为差异比较工具,需设置gitconfig指定sublime为默认diff工具并定义调用命令;2.通过安装命令行工具使subl可在终端使用,并配置gitdifftool.sublime.cmd使用subl-w--wait--diff"$LOCAL""$REMOTE"打开差异文件;3.使用gitdifftool命令即可在SublimeText中以双栏视图查看暂存或提交间的更改;4.该方法依赖SublimeText4的--d

如何在崇高文本中使用emmet? 如何在崇高文本中使用emmet? Sep 21, 2025 am 03:38 AM

EmmetisapluginforSublimeTextthatacceleratesHTMLandCSScodingthroughabbreviations.2.InstallitviaPackageControlbyopeningtheCommandPalette,selecting"InstallPackage,"searchingforEmmet,andinstallingit.3.Afterinstallation,useabbreviationsinHTMLfil

如何在Sublime文本中使用Gitgutter插件? 如何在Sublime文本中使用Gitgutter插件? Sep 17, 2025 am 05:12 AM

GitGutterdisplaysreal-timelinechangesinSublimeTextusingicons: foradded,~formodified,-fordeleted,▶/↺formovedlines;installviaPackageControl,ensureGitisinPATH,enablefeatureslikelivemodeoruntrackedfilemarkersinsettings,anduseright-clickcommandstonavigate

如何在Sublime文本中为Java设置构建系统? 如何在Sublime文本中为Java设置构建系统? Sep 20, 2025 am 12:33 AM

答案:配置SublimeText的Java构建系统需安装JDK并创建自定义构建文件。1.确保JDK已安装且环境变量正确;2.在Sublime中新建构建系统,使用包含javac和java命令的JSON配置;3.保存为Java.sublime-build;4.选择该构建系统并使用Ctrl B编译运行,输出及错误将显示在底部面板,适用于小型项目或学习。

如何在崇高文本中突出显示匹配标签? 如何在崇高文本中突出显示匹配标签? Sep 16, 2025 am 01:11 AM

SublimeText自动高亮匹配的HTML或XML标签对,将光标置于标签上即可显示;若未生效,需检查语法设置是否正确。1.默认支持标签高亮,确保文件语法为HTML或XML。2.可选安装BracketHighlighter插件以增强效果,通过命令面板搜索并安装后可获得更清晰的图标和下划线提示。3.通过首选项调整高亮样式,如修改为实心、轮廓或下划线,并确认标签类型已启用。

如何在崇高文本中使用多个光标? 如何在崇高文本中使用多个光标? Sep 17, 2025 am 01:44 AM

UseCtrl Alt Up/Downtoaddcursorsaboveorbelow.2.PressCtrl DtoselectwordinstancesonebyoneorCtrl Alt Gtoselectallatonce.3.HoldCtrlandclicktoplacemultiplecursorsmanually.4.UseShift Altanddragforcolumnselectiontoeditmultiplelinesvertically.Thesemethodsenab

如何使Sublime Text成为便携式应用程序? 如何使Sublime Text成为便携式应用程序? Sep 20, 2025 am 02:05 AM

SublimeTextbecomesportablebydownloadingthe.zipversion,extractingit,andcreatinga"Data"folderbesidesublime_text.exe;thisstoresallsettingslocally.Whenlaunched,SublimeTextdetectstheDatafolderautomaticallyandsavespackages,preferences,andcachewit

如何在崇高文本中按字母顺序排序行? 如何在崇高文本中按字母顺序排序行? Sep 14, 2025 am 03:04 AM

selectLlinesNoseEdit→sortlinestoSortAlphabetalsalsalsalsalsalsalsalthabetal(ATOZ)OREDIT→SORTLINES(反向)forzta;可选地,AddCustomKeyboardsviapurance→keybindingsforfasteraccess。

See all articles