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"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值