将IPython Shell的工作目录设置为Canopy中正在执行的python文件的工作目录 [英] Setting the working directory of IPython Shell to that of the python file being executed in Canopy
问题描述
我是IPython的新手,我正在使用Canopy发行版。 shell的工作目录的默认值是/ home / username。当我处理需要在我工作的相应文件夹中存在其他文件的文件时,这有点痛苦。
I am new to IPython and I am using the Canopy distribution. The default value of the shell's working directory is /home/username. This is a bit painful, when I work on files which need other files present in the corresponding folder that I am working in.
因此,我总是手动将其更改为文件夹其中存在文件(我正在尝试执行)。
So, I always manually change it to folder in which the file (which I am trying to execute) is present.
我发现这个相关问题,但我的问题略有不同。另外,我无法理解那里提供的答案非常好。
I found this relevant question, but my question is slightly different. Also, I couldn't understand the answer provided there really well.
是否有更短的方法将IPython shell的工作目录设置为文件的工作目录我在干什么?喜欢命令?
Is there a shorter way of setting up the working directory of the IPython shell to that of the file on which I am working? Like a command?
感谢您的帮助
推荐答案
这是我们的功能列表中的功能,但优先级很低。 Canopy提供右键单击上下文菜单选项以切换到当前活动编辑器的目录(请参阅此)。
This is a feature that is on our list of features, but quite low on priority. Canopy provides a right click context menu option to switch to the currently active editor's directory (see this).
此外,作为临时解决方案,您可以使用以下宏来完成工作。
Also, as a temporary work-around, you could use the following macro to get your job done.
# -*- coding: utf-8 -*-
def run():
code_task = get_active_task()
from os.path import dirname
def change_directory():
code_editor = code_task.active_editor
if not code_editor:
return
python_pane = code_task.python_pane
active_dir = dirname(code_editor.obj.path)
python_pane.execute_command(u'cd %s\n' % active_dir)
code_task.on_trait_change(change_directory, 'active_editor')
注意:你必须运行这个宏,每次关闭并重新打开编辑器窗口。
Note: You'll have to run this macro, each time you have closed and reopened the editor window.
请参阅 1 和 2 有关如何创建宏以及有关Canopy中宏的更多信息的说明。
See 1 and 2 for instructions on how to create a macro and for more information about macros in Canopy.
这篇关于将IPython Shell的工作目录设置为Canopy中正在执行的python文件的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!