将工作目录设置为SublimeREPL中活动脚本的位置 [英] Set working directory to location of active script in SublimeREPL
问题描述
我将Sublime Text 3和SublimeREPL一起用于R和Python中的命令.每当启动SublimeREPL时,它将当前工作目录设置为Sublime Text安装目录.在Windows和Mac OSX上都会发生这种情况.通常,我需要从R或Python运行代码,这会打开一个文件,该文件与我正在运行的R
或py
文件位于同一文件夹中.在正常情况下(即不使用Sublime Text),这可以正常工作,因为R或Python解释器知道查找与脚本所在目录相同的目录.
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.
但是,使用SublimeREPL,命令会作为文本传输"到SublimeREPL,因此SublimeREPL不知道程序来自何处.结果,我需要在每个程序中明确指定我要访问的文件的位置,这有点笨拙,尤其是当程序四处移动时(它们确实这样做).
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).
是否有某种方法可以使SublimeREPL在启动时将当前的工作目录设置为与当前活动脚本文件相同的目录?这将是一个很好的解决方法,因为如果我更改为使用位于不同目录的文件,则仅意味着必须重新启动REPL.
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.
推荐答案
I had the same question and found an answer here.
对于Python和IPython,在Packages/User
文件夹中创建SublimeREPL/config/Python/Main.sublime-menu
,在其中指定"cwd": "$file_path"
:
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"
}
}
}
]
}
]
}]
}
]
我想,对于R来说,是相同的.
I guess, for R it is the same.
这篇关于将工作目录设置为SublimeREPL中活动脚本的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!