ipython: Can I provide input to a shell command

前端 未结 4 1577
广开言路
广开言路 2021-01-18 16:12

Can I execute a shell command that requires input in ipython and/or an ipython notebook?

When I execute such a command, I see it\'s prompt, but no apparent way to p

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 16:28

    Sadly, no.

    I couldn't find documentation on this, so I went source-diving. The ipython code that actually performs that transformation is https://github.com/ipython/ipython/blob/master/IPython/core/inputtransformer.py#L208 , specifically:

    def _tr_system(line_info):
        "Translate lines escaped with: !"
        cmd = line_info.line.lstrip().lstrip(ESC_SHELL)
        return '%sget_ipython().system(%r)' % (line_info.pre, cmd)
    

    which, in other words, invokes the underlying shell with everything following the !.

    ipython is probably not what you want -- check out this answer for a Python alternate to include in scripts.

提交回复
热议问题