Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

cmd - Running interactive command line code from Jupyter notebook

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...