Skip to content Skip to sidebar Skip to footer

Build Just One Line In Sublime Text 3 - Python

I am very new to programming and I using 'Learn Python the hard way' and I find it very helpful. One of the questions in the book was to run just one line, which I find impossible

Solution 1:

Here's a little plugin to acomplish what you're asking for:

classRunSelectionsWithPythonCommand(sublime_plugin.TextCommand):

    defrun(self, edit, **kwargs):
        import re
        import tempfile

        chunks = []
        for region in self.view.sel():
            chunks.append(self.view.substr(region))

        if self.view.file_name():
            working_dir = os.path.dirname(self.view.file_name())
        else:
            working_dir = os.getcwd()

        chunks = "\n".join(chunks)
        lines = filter(
            None, [l for l in chunks.split("\n") if l.strip() != ""]
        )
        source_code = "\n".join(lines)

        with tempfile.NamedTemporaryFile(suffix='.py', mode='w', delete=False) as f:
            f.write(source_code)
            window = sublime.active_window()
            window.run_command("exec", {
                "shell_cmd": "python {}".format(f.name),
                "working_dir": working_dir,
                "quiet": False
            })

    defis_enabled(self):
        returnlen(self.view.sel()) > 0

Here's a little demo:

working show

Because you're learning python the hard way I'll leave as an exercise to figure out how to install & use the above plugin... One hint, make sure python is available on the SublimeText process.

Post a Comment for "Build Just One Line In Sublime Text 3 - Python"