简体   繁体   English

如何将标准输入发送到 IPython?

[英]How to send stdin to IPython?

I'm having trouble trying to send data through stdin to IPython, it seems that EOF is interpreted as an exit command for ipython ?我在尝试通过标准输入向 IPython 发送数据时遇到问题,似乎 EOF 被解释为 ipython 的退出命令?

Example:例子:

echo '"hello"' |ipython

Python 3.9.2 (default, Feb 28 2021, 17:03:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: Out[1]: 'hello'

In [2]: Do you really want to exit ([y]/n)?

And the process quits without me doing anything else.这个过程在我没有做任何其他事情的情况下退出。

Is there a parameter to set or something ?有要设置的参数还是什么?


Edit: To make the context clearer, the real scenario where I want to do this is the following:编辑:为了使上下文更清晰,我想要这样做的真实场景如下:

program1 | ipython -i -c "%run script"

program1 would output something (a json), and ipython would run a script that load the thing into something I can use, then leave me to a IPython interpreter where I can play with it. program1 会输出一些东西(一个 json),而 ipython 会运行一个脚本,将这个东西加载到我可以使用的东西中,然后把我留给我可以使用它的 IPython 解释器。

Same as comment.与评论相同。 your question is quite unclear.你的问题很不清楚。

Maybe pipe some python command to a python interperter?也许将一些python命令传递给python interperter?

try this尝试这个

$ (echo 'a=123' && cat ) | ipython -i
Python 3.6.9 (default, Mar 15 2022, 13:55:28)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.3 -- An enhanced Interactive Python. Type '?' for help.

In [1]:
In [2]: print(a)
123

this will execute a=123 and make python into interative mode which take your keyboard input这将执行a=123并使 python 进入交互模式,接受你的键盘输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM