0

I'm using a Jupyter notebook as a tutorial for a command-line tool. However, I noticed that regardless of whether I use a shell command (!) or line magic (%), an error does not cause the cell to fail, and the execution simply continues. For example:

!pip install foo
%pip install foo

Is there any way to cause these errors to be fatal instead of continuing on to the next cell to execute?

3
  • For the sake of everyone involved. I wish you had a better minimal reproducible example here. First, it isn't an either-or -thing in this example case. You should always be using %pip install or you are risking installations that occur where you don't expect. Or arguably worse, contributing to encouraging use by others of approaches that can lead to problems. %pip install has been around since 2019 and even works in JupyterLite. Even Google Colab supports it now. .... Looking past that... the idea would be to add next a try clause where you try import foo. Commented Sep 22 at 16:54
  • <continued> Or better yet, if you really are only demonstrating a command line tool. Then use the bash kernel and stick with using command line approaches. Commented Sep 22 at 17:00
  • I made this a separate comment because I intend to delete it when the system is working... I was going to say that you can test it out here without touching your own system or signing up for anything by using a remote machine served via the MyBinder service. Go there and click the launch binder badge. When the session comes up, you can try to make a new notebook and select the 'Bash' kernel as an option. (It seems to work intermittently right now. They are fixing it actively.) Commented Sep 22 at 17:07

1 Answer 1

2

You want the code in the 'install cell' to be:

%pip install foo
import foo

Then it won't continue on after failing to install.

Technically, this more general solution also allows what you want when using an ipykernel:

%%bash
pip install foo

(For Windows users, you may want to try %%cmd instead of %%bash.)

That should generalize to more command line executions that fail.

However, because of reasons I put in my comments to the original post, this would be bad to teach learners to use pip this way.

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

4 Comments

Pip is just an example of a command-line tool available on everyone's system, the actual command-line tool I want to test is custom. Basically, I want to find a way to cause any arbitrary shell command to cause the entire cell to fail if it is not successful.
Okay. That eliminates my first proposed solution because it is only valid in that case of pip. Did you try the second suggestion with some of your 'real', custom examples? In my tests it stops progression because the kernel gets a CalledProcessError. And it is nice form for learners because it means you at least type the line that corresponds command line command as you would in a console instead of prefacing with weird symbols.
Your second suggestion works! Any idea if this works on Windows as well?
I don't know if that works for Windows. I cannot test. I have added a suggestion for that part of the answer of a different magic command based on looking around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.