【问题标题】:Setting the working directory of IPython Shell to that of the python file being executed in Canopy将 IPython Shell 的工作目录设置为 Canopy 中正在执行的 python 文件的工作目录
【发布时间】:2013-06-03 01:30:00
【问题描述】:

我是 IPython 的新手,我正在使用 Canopy 发行版。 shell 工作目录的默认值是/home/username。当我处理需要其他文件存在于我正在工作的相应文件夹中的文件时,这有点痛苦。

所以,我总是手动将其更改为文件(我正在尝试执行)所在的文件夹。

我找到了这个relevant question,但我的问题略有不同。另外,我无法很好地理解那里提供的答案。

是否有更短的方法可以将 IPython shell 的工作目录设置为我正在处理的文件的工作目录?像命令一样?

感谢您的帮助

【问题讨论】:

    标签: editor ipython enthought


    【解决方案1】:

    您可以在 iPython 中使用cdcd 代表更改目录

    > cd /home/username/my_otherdirectory
    

    【讨论】:

      【解决方案2】:

      这是我们功能列表中的一项功能,但优先级很低。 Canopy 提供了一个右键单击上下文菜单选项来切换到当前活动编辑器的目录(参见this)。

      此外,作为临时解决方法,您可以使用以下宏来完成您的工作。

      # -*- 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') 
      

      注意:每次关闭并重新打开编辑器窗口时,您都必须运行此宏。

      请参阅12,了解有关如何创建宏的说明以及有关 Canopy 中宏的更多信息。

      【讨论】:

        【解决方案3】:

        右键单击 Canopy 中的 python 选项卡,然后从菜单中选择“保持目录与编辑器同步”。它将包含您的源文件的目录设置为您当前的工作目录。您可以通过从右键菜单中选择“更改工作目录”将任何目录设置为您的工作目录

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-17
          • 2022-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多