【发布时间】:2016-04-09 03:48:40
【问题描述】:
我认为在 IPython 中使用 ! 在 shell 命令前添加命令会使系统 shell 实际执行该命令,但似乎情况并非如此。考虑以下,我从/home/Documents/Rx开始,启动IPython,使用命令cd(没有!),然后退出IPython并查看我在哪个目录:
$ 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,但这也不起作用:
$ 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 也没有更改目录;但我本来希望实际的外壳会这样做。最后,使用魔术命令%cd 与cd 具有相同的效果。那么,这三者有什么区别,我如何才能真正告诉系统 shell 在 IPython 中执行命令呢?
【问题讨论】: