Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The !cd command doesn't work normally. #40

Closed
ShuhuaGao opened this issue Jan 30, 2018 · 9 comments
Closed

The !cd command doesn't work normally. #40

ShuhuaGao opened this issue Jan 30, 2018 · 9 comments

Comments

@ShuhuaGao
Copy link

When attempting to change directory with !cd command, it didn't seem to work well. A notebook to reproduce this issue can be found at https://colab.research.google.com/notebook#fileId=1C9x_lQreBVBUvIBOtJD0NHecD6UAHYv4

For your convenience, the screenshots are also attached.
image

image

@craigcitro
Copy link
Contributor

tl;dr: this is a known gotcha.

Commands prefixed with ! are run in a new subshell -- so you're starting a new shell, changing directories, and ... letting the shell process quit. (You can do things like !cd /tmp && ls to run a command in another directory.

By default, IPython enables automagics, which lets you skip the leading %.

This means that the cd command is shorthand for %cd (docs), which changes the directory of the python process itself, not a child subprocess. That's why these changes stick.

I'm closing as WAI, but feel free to ask if that isn't clear.

@ShuhuaGao
Copy link
Author

@craigcitro Thanks very much. I just knew that IPython can work as a shell, because I only used Jupyter notebook in the Windows environment and never tried shell commands before. I thought !cmd was a feature of Colab.

Anyway, got it. Thank you.

@nyck33
Copy link

nyck33 commented Jun 16, 2019

@craigcitro @ShuhuaGao

can someone please clarify what to put instead of !cd foo/bar ?

@nyck33
Copy link

nyck33 commented Jun 16, 2019 via email

@tjvdmolen
Copy link

@craigcitro @ShuhuaGao

can someone please clarify what to put instead of !cd foo/bar ?

%cd foo/bar

@Monster-Gaming-Studios
Copy link

Monster-Gaming-Studios commented Mar 30, 2020

Can somebody please tell me a solution of the code in "How to reproduce" section of issue #1094 (It's closed)

@colaboratory-team
Copy link
Contributor

@Monster-Gaming-Studios see the last comment on this issue above yours: instead of !cd you want %cd. The reason is explained earlier in this issue.

@mrgransky
Copy link

Instead of ! cd my_path/my_dir please try %cd my_path/my_dir!

To confirm, you could now try ! pwd to ensure you're in new directory!

@utkarsh92
Copy link

utkarsh92 commented Jun 1, 2021

Still not able to use environment variables with cd. Basically, I want to store a path as a variable so I don't have to type it again and again. For example:

image

EDIT:
Nvm, I found the solution. Putting it here so others can find it...

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants