Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python script stuck on reading from stdin

For some reason a script that always worked is now failing :( I investigated and trimmed down to something minimal, still not working:

cat a.py:

import sys

for row in sys.stdin.readlines():
  print("hey")
  print(row)

Testing:

>> cat b.csv
a,b,c
1,2,3

>> cat b.csv | python a.py
// hangs for ever, if I Ctrl+C:
for row in sys.stdin.readlines():
  KeyboardInterrupt

Any idea what could be going on? Thanks!

like image 582
Thomas Avatar asked Nov 07 '22 22:11

Thomas


1 Answers

After investigation, it looks like it has to do with the aliases I use.

see commands

Because I have different libraries for python2 and 3 I had

pythonpath2=/usr/local/lib/python2.7/site-packages
pythonpath3=~/Library/Python/3.6/lib/python/site-packages:/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
alias p="unset PYTHONPATH; export PYTHONPATH=${pythonpath3}; python3.7"
alias p2="unset PYTHONPATH; export PYTHONPATH=${pythonpath2}; echo $PYTHONPATH; python"

So I guess before piping in the terminal would pipe into the last command of the alias and a change made it pipe into the first command of the alias (unset) which made it ineffective

like image 111
Thomas Avatar answered Nov 14 '22 23:11

Thomas