Table of Contents
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
Home Development Tools sublime How to run the current Python file in a new terminal from Sublime Text?

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

Aug 21, 2025 am 08:41 AM

To make the current Python file run through a new terminal window in Sublime Text, you need to create a custom build system: 1. Select Tools → Compile System → Create a New Compilation System, and paste the JSON configuration of the corresponding operating system; 2. Select the commands according to the operating system (macOS uses osascript to call Terminal or iTerm2, Linux uses gnome-terminal, etc., Windows uses start cmd or PowerShell); 3. Save as Python-Terminal.sublime-build and select the compilation system; 4. Press Ctrl B (or Cmd B) to run, the terminal will execute the script and keep it open to view the output, and finally complete the entire execution process.

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.

The above is the detailed content of How to run the current Python file in a new terminal from Sublime Text?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

How to use Sublime Text as a diff tool for Git? How to use Sublime Text as a diff tool for Git? Sep 21, 2025 am 01:06 AM

Configure Git to use SublimeText as the difference comparison tool. You need to set gitconfig to specify sublime as the default diff tool and define the call command; 2. Install the command line tool to make subl available in the terminal, and configure gitdifftool.sublime.cmd to use subl-w--wait--diff"$LOCAL""$REMOTE" to open the difference file; 3. Use the gitdifftool command to view changes between temporary storage or submissions in SublimeText in a double column view; 4. This method depends on the --d of SublimeText4

How to use Emmet in Sublime Text? How to use Emmet in Sublime Text? Sep 21, 2025 am 03:38 AM

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

How to use the GitGutter plugin in Sublime Text? How to use the GitGutter plugin in Sublime Text? Sep 17, 2025 am 05:12 AM

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

How to set up a build system for Java in Sublime Text? How to set up a build system for Java in Sublime Text? Sep 20, 2025 am 12:33 AM

Answer: To configure the Java build system for SublimeText, you need to install JDK and create a custom build file. 1. Make sure that the JDK is installed and the environment variables are correct; 2. Build a new build system in Sublime and use a JSON configuration containing javac and java commands; 3. Save as Java.sublime-build; 4. Select the build system and compile and run with Ctrl B. The output and errors will be displayed in the bottom panel, suitable for small projects or learning.

How to highlight matching tags in Sublime Text? How to highlight matching tags in Sublime Text? Sep 16, 2025 am 01:11 AM

SublimeText automatically highlights matching HTML or XML tag pairs, and puts the cursor on the tag to display; if it does not take effect, check whether the syntax settings are correct. 1. Tag highlighting is supported by default to ensure that the file syntax is HTML or XML. 2. Optionally install the BracketHighlighter plug-in to enhance the effect. After searching and installing through the command panel, you can get clearer icons and underline prompts. 3. Adjust the highlight style through preferences, such as modifying it to solid, outline or underscore, and confirm that the label type is enabled.

How to use multiple cursors in Sublime Text? How to use multiple cursors in Sublime Text? Sep 17, 2025 am 01:44 AM

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

How to make Sublime Text a portable app? How to make Sublime Text a portable app? Sep 20, 2025 am 02:05 AM

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

How to sort lines alphabetically in Sublime Text? How to sort lines alphabetically in Sublime Text? Sep 14, 2025 am 03:04 AM

SelectLlinesNoseEdit → Sortlinestosortalphabetically (Atoz) Oredit → Sortlines (Reverse) forzta; optionally, addcustomKeyboardSviapurance → keybindingsforfasteraccess.

See all articles