2017-02-25 48 views
1

我希望SublimeREPL自動加載本地模塊,以便我可以從REPL中調用模塊中的函數,而不必先導入它們。作爲一個例子,util_func.py包含以下內容:配置SublimeREPL導入本地模塊

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

所以我可以打字,打開一個新的REPL後:

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

這可能嗎?提前致謝。

回答

1

原來這很簡單。我只是將以下內容添加到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

+0

歡迎來到SO!感謝您尋找解決方案!將其標記爲保持網站狀態良好的答案,並獎勵自己10分:D –