Sublime Forum

Build system for Python

#1

I use the usual build system for Python with CTRL + B, and the results are displayed in the bottom “build panel” :

I’d like to have a second build system for Python with keyboard shortcut F5, that would open a new terminal and run the Python script there :

How is it possible to add such a second build method for the same extension (.py) ?

0 Likes

Run Python and Keep Python Command Line Open After
Use ctrl+b to open build window if running
Execute python in new terminal MAC
#2

I don’t think you can do it with the build system, because of the way it works.
However, it should be trivial to implement as a plugin. Simply open a new shell process with a certain command that interpolates the file name/path.

0 Likes

#3

[quote=“prmaple”]I don’t think you can do it with the build system, because of the way it works.
However, it should be trivial to implement as a plugin. Simply open a new shell process with a certain command that interpolates the file name/path.[/quote]

I tried Preferences > Key bindings - User and thought I could add a keyboard shortcut :

{ "keys": "f5"], "command": "python", "args": { ?? }}

But I’m currently unsuccessful.

Can you give some details about how to implement it as a plugin? (you said it’s trivial)

0 Likes

#4

Save the following as “python_run.py” in your “Packages/User” folder

[code]import sublime
import sublime_plugin
import os

class PythonRunCommand(sublime_plugin.WindowCommand):
def run(self):
command = ‘python {} && pause’.format(
sublime.active_window().active_view().file_name()
)

    os.system(command)

[/code]

set a key binding

{ "keys": "f2"], "command": "python_run" }

and here we go:

0 Likes

#5

Thanks a lot!
Working with:

[quote]import sublime
import sublime_plugin
import os

class PythonRunCommand(sublime_plugin.WindowCommand):
def run(self):
os.system(’“C:\Python27\python.exe” %s’ % sublime.active_window().active_view().file_name())
[/quote]

0 Likes

#6

Last thing:

The running terminal (containing Python process) seems to be “attached” to Sublime, i.e. while the Python script is running in the outside terminal, Sublime is non-responding. :neutral_face:

How to make the process independant from Sublime?

0 Likes

#7

Solved with

[code]import sublime
import sublime_plugin
import subprocess

class PythonRunCommand(sublime_plugin.WindowCommand):
def run(self):
command = ‘cmd /k “C:\Python27\python.exe” %s’ % sublime.active_window().active_view().file_name()
subprocess.Popen(command)
[/code]

0 Likes

#8

all sorted? nice

0 Likes

#9

Since it seems you are running windows, check this plugin out:
https://github.com/Twizzledrizzle/Send-to-Shell

(not available in the package control currently)

You can send the selected text to a running IPython terminal, running in whatever shell you use most.

0 Likes

#10

is it possible to do the same but on Linux(xubuntu)???
obviously the code as it is wont work on Linux but can it be edited to work on linux?

0 Likes

#11

is it possible for that code to be edited in such way that it could work on Linux???

0 Likes