配置 SublimeREPL 导入本地模块

Configure SublimeREPL to import local module

我希望 SublimeREPL 自动加载本地模块,这样我就可以从 REPL 调用模块中的函数,而无需先导入它们。例如,util_func.py 包含以下内容:

import datetime
fdate = lambda: datetime.date.today().strftime('%Y-%m-%d')

所以我可以在打开一个新的 REPL 后立即输入:

>>> fdate()
'2017-02-24'

这可能吗?提前致谢。

原来这很简单。我刚刚将以下内容添加到 Packages/User/SublimeREPL/config/Python/Main.sublime-menu。选择此添加定义的菜单选项将执行 util_func.py 并放入 REPL。

[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"caption": "Python",
                "id": "Python",

                 "children":[
                    {"command": "repl_open",
                     "caption": "Python",
                     "id": "repl_python",
                     "mnemonic": "P",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python", "-i", "-u", "PATH TO utility_func.py"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    }
                ]}
            ]
        }]
    }
]

记得把上面的PATH TO utility_func.py换成正确的路径。