Why does importing subprocess change my output?
Asked Answered
T

2

12

I noticed the following using Python 2.5.2 (does not occur using 2.7):

#!/usr/bin/python

import sys

for line in sys.stdin:
   print line,

Output:

$ echo -e "one\ntwo\nthree" | python test.py
$ one
$ two
$ three

as expected. However, if I import subprocess to the this script:

#!/usr/bin/python

import sys
import subprocess

for line in sys.stdin:
   print line,

Output:

$ echo -e "one\ntwo\nthree" | python test.py
$ two
$ three

What happened to the first line of output?

Update:

I think I may have discovered the root of the problem. I had a file named time.py in my cwd. A time.pyc is being created every time I run the script with subprocess imported, suggesting that ./time.py is also being imported. The script runs normally if I delete the .pyc and time.py files; however, there is still the question of why a subprocess import would cause ./time.py to be imported as well?

I have narrowed it down even further to the exact line in time.py that causes the strange behaviour. I have stripped down the working dir and file content to just that which affects the output:

test.py

#!/usr/bin/python

import sys
import subprocess

for line in sys.stdin:
   print line,

time.py

#!/usr/bin/python

import sys

for line in sys.stdin:
   hour = re.search(r'\b([0-9]{2}):', line).group(1)

Running test.py with any kind of input results in the first line of output being omitted and time.pyc being created.

Tami answered 29/10, 2012 at 15:19 Comment(10)
I can not confirm it on 2.4.3: "echo -e "one\ntwo\nthree" | python test.py" results in "-e one two three" for the last one example.Jeanne
@tb-: You must use the bash shell. echo is a builtin, and tcsh (for example) does not support the -e flag.Sporades
hm, I can't actually confirm this at all: python2.5.6 on UbuntuGangboard
@sharth with bash, the result is the same (without the -e)Jeanne
@SilentGhost: I'm getting these results with 2.5.2 on Debian 5.0.6. Python 2.6.6 and 2.7.1 on the same machine behave as expected.Tami
i have tried it with python2.6 and 2.7 and i get all the lines outputted correctly as you'd expect. it's not useful i know, but maybe there's something else you're missingMafala
cannot confirm on python2.4.4 eitherGangboard
Does your time.py file do any input when run? If so, it's probably eating your "one" line. It's generally a bad idea to name your own modules with the same name as something in the standard library for this reason! Packages can help a bit.Pulsatory
@Blckknght: Well, time.py doesn't run; it's not a valid script as it is.Tami
@user1227038: subprocess needs time in order to be able to wait. Anyway. This honestly is not the question that deserves time spent on it or upvotes it received.Gangboard
S
1

Sounds like your local time.py will be imported instead of the global time module. You might want to rename it, or at least start checking if it was run as a script or imported as a module.

This will prove it for you if you want to test it.

#!/usr/bin/python

import sys

# Test that script was run directly
if __name__=='__main__':
    for line in sys.stdin:
       hour = re.search(r'\b([0-9]{2}):', line).group(1)
else:
    print 'Imported local time.py instead of global time module!'
    sys.exit(1)
Sepulchre answered 30/10, 2012 at 17:26 Comment(0)
P
-2

print line, before 2.7 did not put out a newline so it overwrote the first line. Lose the comma and the result will be the same.

Page answered 29/10, 2012 at 15:34 Comment(3)
why is line two\n was not overwritten? Besides, import subprocess shouldn't have affected it.Gangboard
The behavior of the trailing comma was not changed in 2.7Dialect
The purpose of the trailing comma in print is to omit the newline, docs.python.org/2/reference/… "A '\n' character is written at the end, unless the print statement ends with a comma"Astronomy

© 2022 - 2024 — McMap. All rights reserved.