Running interactive command line code from Jupyter notebook

前端 未结 4 1648
死守一世寂寞
死守一世寂寞 2020-12-14 15:32

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

! mkdir ...
! python f         


        
4条回答
  •  攒了一身酷
    2020-12-14 16:14

    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.

提交回复
热议问题