无法在 jupyternotebook(或 jupyterlab 会话)中更改目录(shell 命令)

BCA*_*Arg 6 ipython python-3.x jupyter-notebook

我可以从打开的 jupyterlab(或 jupyternotebook)会话中运行 shell 命令,并在 shell 命令前加上感叹号,如下所示:

!mkdir /new_folder
Run Code Online (Sandbox Code Playgroud)

这一点,以及其他命令,如lspwd工作,但如果我尝试更改目录cd,如下图所示

!cd /path/to/mydir
Run Code Online (Sandbox Code Playgroud)

这不起作用,我注意到当前工作目录将始终是保存我的 jupyter notebook (.ipynb) 的目录。

如果我这样做也很奇怪:

!cd /path/to/mydir && pwd
Run Code Online (Sandbox Code Playgroud)

我会被/path/to/mydir打印出来,但如果,在下面的单元格上我做

!pwd

我将获得保存 jupyternotebook 的当前目录,即显然我最终无法更改!cdjupyternotebook 中的工作目录。

有谁知道是什么问题?

swa*_*hai 7

您不能用于!cd从 Jupyter notebook 导航文件系统。原因是!Jupyter notebook 代码单元中的shell 命令(以符号开头)是在临时子 shell 中执行的。如果您想更改工作目录,可以使用%cd魔术命令:

!pwd
/d/swatchai_works/tutorial/jupyter

%cd ..
/d/swatchai_works/tutorial

!pwd
/d/swatchai_works/tutorial

%cd jupyter
/d/swatchai_works/tutorial/jupyter
Run Code Online (Sandbox Code Playgroud)