Odd Behaviour When Running Scripts From The CMD Line

Hello,

I am currently retesting a script directly from the CMD line. Note that when I run it using IDLE, the outputs are as expected. Not so from the CMD line.

The test script is:

# teststreams.py

def interact():
    print('Hello stream world')                     # print sends to sys.stdout
    while True:
        try:
            reply = input('Enter a number>')        # input reads sys.stdin
            num = int(reply)
            print("%d squared is %d" % (num, num ** 2))
        except EOFError:
            break                                   # raises an except on eof
        except ValueError:
            break

    print('Bye')
    
if __name__ == '__main__':
    
    interact()                                      # when run, not imported

Here is how I tested the script from the command line:

C:\Desktop\Temp_Directory>py teststreams.py
pre if
Hello stream world
pre reply
Enter a number>post reply
8 squared is 64
pre reply
Enter a number>post reply
6 squared is 36
pre reply
Enter a number>Bye
post if

Before, I used to get the following (the correct expected output as opposed to the output above):

Hello stream world
Enter a number>12
12 squared is 144
Enter a number>10
10 squared is 100
Enter a number>^Z
Bye

It appears that it is unexpectedly adding: pre if, pre reply, post reply, and post if.

Does anyone know what would cause this?

In your post, the code has the line:

# teststream.py

Is “teststream.py” the actual name of the file?

I ask because your output says:

C:\Desktop\Temp_Directory>py teststreams.py

Is there also a file called “teststreams.py”?

Are there 2 files with almost the same name?

Hello,

was a typo. it is teststreams.py.

I suggest adding:

print(__file__)

and then testing again.

Are they definitely running the same file?

Did you ever have lines that printed “pre if”, etc, in the file?

where specifically? I added it just above:

def interact():

There is only one script with that name in the directory.

No, this is the very first time.

Update:

I made a copy of the teststreams.py module and named it something else.

py new_file_with_original_contents.py

After running this new module, it worked as expected.

Very odd.

Then you weren’t running the code you thought you were. There are a few possible reasons for this, but the most obvious ones (file name confusion, different directories with the same named file, etc) have already been covered, so I’ll just mention this one: Python retains a precompiled copy of scripts that you import, and can run those without needing to look at the original .py file. Normally, there’s a check to make sure the file hasn’t changed; but it is possible that something went wrong. (This is Windows. I’ve seen much MUCH weirder things. [1]) Check to see if there’s a file called teststreams.pyc or similar, possibly in a subdirectory called __pycache__.


  1. And I’ve seen things on Linux that are weirder still, like how running one program and then shutting it down can leave me with 200 load average on an idle system. But I digress. ↩︎

2 Likes

Yup, you hit that nail right on the head. I just confirmed it. It did have this
file. Doh!

2 Likes