54

There is an interesting option in Ipython Jupyter Notebook to execute command line statements directly from the notebook. For example:

! mkdir ... ! python file.py 

Moreover - this code can be run using os:

import os os.system('cmd command') 

but how do I run interactive shell commands. For example:

!conda install package 

may require future input ([Y]/N) or folder location, but won't accept further input.

3
  • Quick question: Which OS are you asking about? For this question, I think it matters. Apparently you can enter the Bash shell if you want, but that won't make much difference if you're on Windows. Commented Dec 18, 2017 at 21:26
  • I guess it is relevant for the 3 of them, but I'm specifically interested in Mac OS system. Commented Dec 18, 2017 at 21:36
  • Mac can probably have a bash shell. I'll look into it tomorrow. Commented Dec 18, 2017 at 22:40

6 Answers 6

21

the !commandsyntax is an alternative syntax of the %system magic, which documentation can be found here.

As you guessed, it's invoking os.system and as far as os.system works there is no simple way to know whether the process you will be running will need input from the user. Thus when using the notebook or any multi-process frontend you have no way to dynamically provide input to your the program you are running. (unlike a call to input in Python we can intercept).

As you explicitly show interest in installing packages from the notebook, I suggest reading the following from Jake Van Der Plas which is a summary of a recent discussion on the subject, and explain some of the complications of doing so. You can of course go with --yes option of conda, but it does not guarantee that installing with conda will always work.

Note also that !command is an IPython feature, not a Jupyter one.

Sign up to request clarification or add additional context in comments.

Comments

12

Assuming you are asking about interactivity, there is something you can try.

If you ever wondered how Jupyter knows when the output of a cell ends: well, it apparently does not know, it just dumps any captured output into the most recently active cell:

import threading,time a=5 threading.Thread(target=lambda:[print(a),time.sleep(20),print(a)]).start() 

(deliberately shorter-than-nice example, as this is just side-info. While the 20-second wait is running you have time to activate another cell, perhaps via issuing an a=6)

This can be used to get the output of some console code to the screen, while controlling it from the main thread:

import sys,threading,subprocess proc=subprocess.Popen('/bin/sh',stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.STDOUT) pout=proc.stdout pin=proc.stdin def outLoop(): running=True while(running): line=pout.readline().decode(sys.stdout.encoding) print(line,end='') running='\n' in line print('Finished') threading.Thread(target=outLoop).start() 

Then you can isssue commands, like

pin.write(b'ls -l\n') pin.flush() 

and

pin.write(b'exit\n') pin.flush() 

Even b'ls\nexit\n' works, that is why outLoop is so long (a simple while(proc.poll() is None)-print(...) loop would finish sooner than it has grabbed all output.

Then the whole thing can be automated as:

while(proc.poll() is None): inp=bytearray(input('something: ')+'\n',sys.stdin.encoding) if(proc.poll() is None): pin.write(inp) pin.flush() 

This works well on https://try.jupyter.org/, but obviously I did not want to try installing conda packages there, so I do not know what happens when conda asks a question.

A lucky thing is that the input field stays at the bottom of the cell (tested with ls;sleep 10;ls). An unlucky thing is that the input field needs an extra entry at the end to disappear (and that is already the 'nice' way, when it was a simple while(...)-write(bytearray(input()))-flush() 3-liner, it was exiting with an exception.

If someone wants to try this on Windows, it works with 'cmd', but I suggest using a hardcoded 'windows-1252' instead of sys.stdin/out.encoding: they say UTF-8, but a simple dir command already produces output which is neither UTF-8 nor ASCII (the non-breakable space between the 3-digit groups in sizes is a 0xA0 character). Or just remove the decode part (and use running=0xA in line)

1 Comment

Thanks for the detailed answer. Is there a way to make this mechanism work for an interactive shell script (which works based on the options provided by the user) from Jupyter?
9

Good option for most of the commands I've encountered is using non-interactive args. E.g. in the above case:

conda install package -y 

If you absolutely need to feed the prompts, you can use printf hack, e.g.:

printf 'y\n' | conda install package 

This supports multiple inputs, you separate them by '\n'

3 Comments

It does not seem as clear as it could be from the answer above, but the most important part is there. To automatically approve the installation of conda package within a Jupyter notebook, you would use the -y argument. So the way to install a package using conda within a cell in a (IPython) Jupyter would be to type: !conda -y install package.
This works for me!
This answer is incomplete for the question, but it solved my use case and probably most others
2

This seems to work for me

import sys !{sys.executable} -m pip install panda 

enter image description here

2 Comments

There's now modern magic commands that will replace this when inside a notebook for pip and conda, see here. As Matt's answer, currently the top rated one, points out all that you posted here is best replaced with %pip install panda. Much easier now. However, the OP question was actually about conda with -y or --yes option, which works with %conda install magic command that Matt's top-rated answer already points out. So this is valid in a cell: %conda install scipy -y
What would be the equivalent for pip? Thanks
1

I know this is an old post, but I didn't see this mentioned:

!yes | conda install package 

This pipes a 'y' to respond to any pending "y/n?" question. Obviously, use with care - only when you're absolutely sure "yes" is the right response.

1 Comment

please consider to formar the terminal commands or, code with backtick. also, would be nice if you explain what does that command do
1

I'm posting this as an answer. It's not a good answer, but the way I'd handle the problem is to write a bash script to be run in the background. I've looked into the '!' operator and it doesn't seem to have a lot of documentation. I can't even find it in the Jupyter source. This article:

Safari book about Jupyter Predecessor and component IPython

suggests that this is simply the way things were before and probably forever shall be. Unless you want to hack into the Magic Commands part of the Jupyter Notebook and fix it yourself.

That said, given that with a little bash programming (It's simple and focused) you can do what you're trying to do, you might consider that route. Especially if you need results enough to put reputation on it.

If you want to look into running bash scripts with against an expected response this answer is what you're looking for Bash Script Editing

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.