本文介绍了在IPython中cd vs!cd vs%cd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在IPython中使用预先添加一个shell命令会使系统shell实际执行命令,但似乎并非如此。考虑以下内容,我从 / home / Documents / Rx 开始,启动IPython,使用命令 cd (与没有),然后退出IPython并查看我所在的目录:

I thought prepending a shell command with a ! in IPython makes the system shell actually execute the command, but it seems this isn't the case. Consider the following, where I begin in /home/Documents/Rx, start IPython, use the command cd (with no !), then exit IPython and see which directory I am in:

$ pwd
/home/Documents/Rx
$ ipython
Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.3 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: cd Papers/
/home/Documents/Rx/Papers

In [2]: pwd
Out[2]: u'/home/Documents/Rx/Papers'

In [3]: exit()
$ pwd
/home/Documents/Rx

如你所见,我实际上没有改变目录;仅在IPython shell中。我认为这是因为我没有在IPython中使用!cd ,但这不起作用:

As you can see, I did not actually change directories; only in the IPython shell. I thought this was because I didn't use !cd in IPython, but that doesn't work, either:

$ pwd
/home/Documents/Rx
$ ipython
Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.3 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: !cd Papers/

In [2]: pwd
Out[2]: u'/home/Documents/Rx'

In [3]: exit()
$ pwd
/home/Documents/Rx

请注意,在这种情况下,IPython shell也没有更改目录;但我本来期望实际的shell这样做。最后,使用magic命令%cd cd 具有相同的效果。那么,三者之间有什么区别,我怎样才能告诉系统shell在IPython中执行命令?

Note that in this case, the IPython shell didn't change directories, either; but I would have expected the actual shell to do so. Finally, using the magic command %cd has the same effect as cd. So, what's the difference between the three, and how can I actually tell the system shell to execute a command in IPython?

推荐答案

!command 不会使shell运行命令。它使 a shell运行该命令。它不是你运行IPython的shell;你无法从IPython向该shell发送命令。这是一个新的shell。

!command doesn't make "the shell" run the command. It makes a shell run the command. It's not the shell you ran IPython from; you can't send commands to that shell from IPython. It's a new shell.

当你执行!cd 时,你启动一个新的shell,它改变了它自己的当前工作目录并立即关闭。这对IPython或从你启动IPython的shell没有影响。

When you do !cd, you launch a new shell, which changes its own current working directory and promptly shuts down. This has no effect on IPython or the shell you launched IPython from.

当你执行 cd %cd ,告诉IPython更改自己的工作目录。这将在您的IPython会话期间持续存在,但它仍然对您启动IPython的shell没有影响。当您停止IPython时,该shell仍将位于启动IPython时的任何目录中。

When you do cd or %cd, you tell IPython to change its own working directory. This will persist for the duration of your IPython session, but it still has no effect on the shell you launched IPython from. When you stop IPython, that shell will still be in whatever directory it was when you started IPython.

您无法从IPython更改父shell的工作目录。

You can't change the parent shell's working directory from IPython.

这篇关于在IPython中cd vs!cd vs%cd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:39