Search code examples
pythonsublimetext2sublimetext3sublimerepl

Configure SublimeREPL to import local module


I'd like SublimeREPL to load a local module automatically, so that I can call functions in the module from the REPL without importing them first. As an example, util_func.py contains the following:

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

So I can type, immediately after opening a new REPL:

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

Is this possible? Thanks in advance.


Solution

  • Turns out this was very simple. I just added the following to Packages/User/SublimeREPL/config/Python/Main.sublime-menu. Selecting the menu option defined by this addition will execute the util_func.py and drop into a 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"}
                            }
                        }
                    ]}
                ]
            }]
        }
    ]
    

    Remember to replace PATH TO utility_func.py in the above with the correct path.