问题
I use Sublime Text 3 with SublimeREPL for my commands in both R and Python. Whenever SublimeREPL is booted up, it has the present working directory set to the Sublime Text installation directory. This occurs on both Windows and Mac OSX. Often, I need to run code from R or Python which opens a file that is located in the same folder as the R
or py
file that I'm running. Under normal circumstances (i.e. not using Sublime Text), this works fine, as the R or Python interpreter knows to look in the same directory as the script is located.
However, with SublimeREPL, commands are 'transferred' as text to the SublimeREPL, so SublimeREPL has no idea where the program came from. As a result, I need to explicitly specify the location of the file I am trying to access in each of my programs, which is a little unwieldy, especially if my programs move around (which they do).
Is there some way of making SublimeREPL set the present working directory, on bootup, to the same directory as the currently active script file? This would be a fine workaround, as it would simply mean having to restart the REPL if I changed to using a file located in a different directory.
回答1:
I had the same question and found an answer here.
For Python and IPython, in your Packages/User
folder, create SublimeREPL/config/Python/Main.sublime-menu
, where you specify "cwd": "$file_path"
:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
{
"caption": "Python",
"id": "Python",
"children":[
{
"command": "repl_open",
"caption": "Python - Anaconda",
"id": "repl_python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["/path/to/Anaconda/python", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
{
"command": "repl_open",
"caption": "IPython - Anaconda",
"id": "repl_python_ipython",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": ["/path/to/Anaconda/python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"
}
}
}
]
}
]
}]
}
]
I guess, for R it is the same.
来源:https://stackoverflow.com/questions/26608408/set-working-directory-to-location-of-active-script-in-sublimerepl