问题描述
我对编程非常陌生,我使用艰难地学习 Python",我发现它非常有帮助.
I am very new to programming and I using "Learn Python the hard way" and I find it very helpful.
书中的一个问题是只运行一行,我发现在 Sublime Text 3 中这是不可能的.我试过谷歌,但我只能找到 Sublime Text 2 和一些我无法找到的解决方案工作.
One of the questions in the book was to run just one line, which I find impossible to do in Sublime Text 3. I have tried to google, but I can only find for Sublime Text 2 and some solutions that I was unable to make work.
我不只是使用 Sublime Text 3 附带的默认构建,有没有办法在 Sublime 中标记 .py 文件的某些行并仅构建这些行?当我按cmd+b"时,而不是整个文件?
I don't just use the default build that comes with Sublime Text 3, is there a way to mark some lines of the .py file in Sublime and build just those lines? Instead of the whole file when I press "cmd+b"?
任何帮助将不胜感激,谢谢.
Any help would be appreciated, thank you.
推荐答案
这里有一个小插件可以满足您的要求:
Here's a little plugin to acomplish what you're asking for:
class RunSelectionsWithPythonCommand(sublime_plugin.TextCommand):
def run(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
})
def is_enabled(self):
return len(self.view.sel()) > 0
这是一个小演示:
因为您正在艰难地学习python,所以我将作为练习来弄清楚如何安装&使用上面的插件...一个提示,确保 python 在 SublimeText 进程中可用.
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.
这篇关于在 Sublime Text 3 中只构建一行 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!