Sublime Text 3环境使用SublimeREPL插件运行Python文件

Sublime Text 3是一款轻便的IDE软件,适合Python开发的利器,虽不及Pycharm专业对口,但是轻巧易用,功能齐全。但是使用默认的build system执行需要输入的脚本(代码包含input()方法)时,默认的build方法并不好用,这个时候需要用到SublimeREPL插件。下面介绍SublimeREPL插件的安装和配置。

  1. 可使用Package Control安装SublimeREPL插件,
    具体安装方法请参考https://blog.csdn.net/lylfv/article/details/81452582

  2. 使用SublimeREPL插件执行python文件
    Tools-> SublimeREPL->Python->Python-RUN current file

  3. 测试代码(Python3.6.1测试通过)

def multi(x):
	return x ** 3

if __name__ == '__main__':
	var = int(input('Please input a number: '))
	print(multi(var))

优化: 通过F5键代替上述步骤2

Sublime Text 3-> Preferences-> Key Bindings
输入下列内容并保存:

[ 
   	{
     
    "keys":["f5"],
    "caption": "SublimeREPL: Python - RUN current file",
    "command": "run_existing_window_command", "args": {"id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"}
     
    }
]

附录:SublimeREPL配置使用Python3

  1. Preferences-> Browse Packages

  2. 打开SublimeREPL->config文件夹

  3. 新建Python3文件夹,并添加下列文件(可参考Python文件夹)
    Default.sublime-commands
    Main.sublime-menu

  4. 修改Default.sublime-commands,内容如下:

[
    {
        "caption": "SublimeREPL: Python3",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "caption": "SublimeREPL: Python - PDB current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python_pdb",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "caption": "SublimeREPL: Python3 - RUN current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3_run",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "command": "python_virtualenv_repl",
        "caption": "SublimeREPL: Python - virtualenv"
    },
    {
        "caption": "SublimeREPL: Python - IPython",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python_ipython",
            "file": "config/Python3/Main.sublime-menu"
        }
    }
]

5.修改Main.sublime-menu,内容如下:
[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"caption": "Python3",
                "id": "Python3",

                 "children":[
                    {"command": "repl_open",
                     "caption": "Python3",
                     "id": "repl_python3",
                     "mnemonic": "P",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-i", "-u"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "python_virtualenv_repl",
                     "id": "python_virtualenv_repl",
                     "caption": "Python - virtualenv"},
                    {"command": "repl_open",
                     "caption": "Python - PDB current file",
                     "id": "repl_python_pdb",
                     "mnemonic": "D",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-i", "-u", "-m", "pdb", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "repl_open",
                     "caption": "Python - RUN current file",
                     "id": "repl_python_run",
                     "mnemonic": "R",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-u", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "repl_open",
                     "caption": "Python - IPython",
                     "id": "repl_python_ipython",
                     "mnemonic": "I",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "autocomplete_server": true,
                        "cmd": {
                            "osx": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "linux": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "windows": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
                        },
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {
                            "PYTHONIOENCODING": "utf-8",
                            "SUBLIMEREPL_EDITOR": "$editor"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]

你可能感兴趣的:(python,sublime,text,3)