+4

Running python through SublimeREPL with command line arguments

Stephen Andrews 5 years ago • updated 4 years ago 3

I am running python through SublimeREPL, but my code takes command line arguments. In my old editor, DrPython, you could set variable in the editor, eg. $args, to be a string which was passed to the python code.

I am using SublimeREPL because i make a lot of use of pdb and like the interactive debugging features.

This is how i currently run my python code via a shortcut

{ "keys": ["f8"], "command": "repl_open",

  "caption": "Python",

  "mnemonic": "p",

  "args": {

  "type": "subprocess",

  "encoding": "utf8",

  "cmd": ["python", "-i", "-u", "$file"],

  "cwd": "$file_path",

  "syntax": "Packages/Python/Python.tmLanguage",

  "external_id": "python"

  } }

what i would like is to change one line to

  "cmd": ["python", "-i", "-u", "$file", "$args"],

where $ for example

You would be able to set the $args variable to be a property of the view which can be set by a command and then revert to a null string when the view is closed.

I am not very familiar with customising Subline so my questions are

1) Is there a package that already does this or something similar?

2) If not, would it be possible to implement

3) If so, any suggestion about how to do it

4) Am i calling python the wrong way? Is there a different way to run python scripts which would make my life easier?

and finally..

5) since no one else seems to have this problem, is it poor form to use command line arguments for python? I like the arguments because i can use them to build nice bash scripts to run a series of simulations with different names and properties. Is there a better way to do this?

 
Your only possibility to parse values/arguments that are not specified in Sublime's key binding extension (like "$file") is to modify the command itself.
While it would be nice to have Sublime extend your (view's) settings in keybindings, build system etc. this can be done inside SublimeREPL. You should open a ticket there.

Hi Stephen,

Did you find any resolution to your question? If so, please could you post an update?


Hi Mark


My solution to this problem was to stop using command line arguments. However, I do know of a good work-around which you could use if you are in a situation where you need to use them.


In the most recent version of SublimeREPL for ST3, there is a shell environment which will allow you to run the script easily in the current directory without opening a different window. Copy the below code into your user keyboard shortcuts file and replace "f10" with whichever key you like.


In linux the shell has memory so you can use the up arrow to retrieve the history, I don't think this works in windows, never tried it on mac. 

       

        { "keys": ["f10"], 

                    "command": "repl_open", 

                     "caption": "Shell",

                     "id": "repl_shell",

                     "mnemonic": "s",

                     "args": {

                        "type": "subprocess",

                        "encoding": {"windows": "$win_cmd_encoding",

                                     "linux": "utf-8",

                                     "osx": "utf-8"},

                        "cmd": {"windows": ["cmd.exe"],

                                "linux": ["bash", "-i"],

                                "osx": ["bash", "-i"]},

                        "cwd": "$file_path",

                        "cmd_postfix": "\n", 

                        "env": {},

                        "suppress_echo": true, 

                        "syntax": "Packages/Text/Plain text.tmLanguage"

                        },

         },


I hope this helps.