Repl with Sublime Keybinding to use Python interactively

This is how I solved a problem I had finding a way to key bind syblime to repl so that I can run python scripts directly from Python once you install sublime repl with package control.

What happened

Perhaps due to the last update (sublime text 3.2), I lost the key binding to sublime REPL, even if the plugin was still there. So I had to find how to bring it back so that when you click on ctrl + b sublime executes the Python code, that is super helpful to try your code quickly. After searching on the net, I figured out how to do it, modifying something I found on stackoverflow. I want to share it with you, as I found out that other people had the same problem, so here it is.

Put this in preferences / keybinding

[

{"keys": ["ctrl+b"], "command": "repl_open",
 "caption": "Python - RUN current file",
 "id": "repl_python_run",
 "mnemonic": "d",
 "args": {
    "type": "subprocess",
    "encoding": "utf8",
    "cmd": ["C:/Users/giova/AppData/Local/Programs/Python/Python37-32/python.exe", "-u", "-i", "$file_basename"],
    "cwd": "$file_path",
    "syntax": "Packages/Python/Python.tmLanguage",
    "external_id": "python",
    "extend_env": {"PYTHONIOENCODING": "utf-8"}
        }}
]

 

How to save on build (it did not worked before I understood this)

I had a problem: repl was working when I pressed ctrl + b, but it did not save automatically the latest change, so that it run the last file saved and not the last edited, even if there was the Save All On Build option checked in the tool menu. Finally I found out why. I just needed to change the \\ with / in the path of the python.exe. So, when you import sys and do sys.path to see where the location of python.exe is, copy the path, but remember to change the \\ with /.

https://youtu.be/1ZCapYOFUQM

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.