Responding to Shell prompt from Jupyter Notebook
Asked Answered
S

3

12

When using the ! shortcut to execute shell commands from a Notebook, how can I respond to subsequent prompts?

For example: !read varname Seems to hang the kernel indefinitely and I can't run another command to respond.

Anticipating the prompt in one cell like this doesn't work either:

!read varname
!my_answer
!echo $varname

The cell hangs with [*]

(these are simplified examples, the actual use case needs to respond to a 'y/n' prompt)

Sweatbox answered 7/2, 2019 at 21:12 Comment(3)
Don't think it's possible, check github.com/ipython/ipython/issues/514. You can still use -y or -n option explicitly in the command.Tannenwald
Thank you! The -y option works for my use case, which is uninstalling a package: !pip uninstall openpyxl -y. I'll leave this question open since this is not a solution to the question as posted.Sweatbox
In modern Jupyter, that pip should be used in conjunction with the magic symbol and not an exclamation point. See my comment here below how the magic version of pip was added in 2019 to overcome shortcomings that can arise with using pip with an exclamation point.Essie
R
2

The input stream of the temporary bash subshell is not connected to your browser. Sending a command to the jupyter kernel is one-way -- there is no interaction. The bash is no exception here.

State changes -- like changing the working directory with a 'cd' command -- require special implementation effort: 'magic commands'.

Rossi answered 30/12, 2019 at 12:35 Comment(0)
R
1

For my usecase to uninstall a package from jupyter notebook the below command worked

!pip uninstall package_name -y
Regimen answered 9/7 at 0:4 Comment(3)
These days, that should be %pip .... The magic variation of the command was added in 2019 to ensure action occurs in the environment where kernel is running that is backing the active notebook. The exclamation point alone doesn't ensure that and may therefore lead to issues. You should endeavor to suggest current best practices in modern Jupyter, Plus, adopt using them for the best experience yourself. See more about the modern %pip ... command here. ...Essie
<continued> The second paragraph here goes into more details about why the exclamation point may lead to issues.Essie
thanks @Essie for adding more context and elaborating on best practicesRegimen
C
0

I used a single exclamation mark and then right after that I added my response Something like this !command !response both on the same line

Corky answered 8/6 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.