如何从Sublime Text中运行当前的Python文件?
要让当前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)运行,终端将执行脚本并保持打开以便查看输出,最终完成整个执行流程。
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.

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.
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
.

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
withxterm
orkonsole
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
(orCmd 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
orpython3
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 forcmd
.
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中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

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

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

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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

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

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

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

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

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

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

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