Issue
I would like to pipe some data into the ipython
terminal client, run a command, and then stay in the session.
In terms of passing an argument into IPython, running a command, and keeping the session alive, this works:
[~]$ ipython -i -c "import sys; print(sys.argv[-1])" "my,testcsv\n1,2,3"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]:
But adding a pipe both raises an exception in Python and immediately quits back to the shell:
[~]$ echo "my,test,csv\n1,2,3" | ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
import sys; print(sys.argv[-1])
In [1]: File "<ipython-input-1-f826f34ba2b7>", line 1
my,test,csv\n1,2,3
^
SyntaxError: unexpected character after line continuation character
In [2]: Do you really want to exit ([y]/n)?
[~]$ ^
And using xargs
processes the input correctly, but it also immediately quits back to the shell:
[~]$ echo "my,test,csv\n1,2,3" | xargs -0 ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]: Do you really want to exit ([y]/n)?
[~]$
Is there a way to accomplish what I'm after here?
Solution
I think it's exiting when it reads EOF from stdin. true | ipython -i
also exits immediately. For that matter, so does true | python -i
.
Instead you could use process substitution to get the command's stdout as a file.
tmp.py
:
import sys
fname = sys.argv[-1]
with open(fname) as f:
print(f.read())
$ ipython --no-banner -i tmp.py <(echo "foo")
foo
In [1]:
Answered By - wjandrea
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.