# Odd Behaviour When Running Scripts From The CMD Line

**URL:** <https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099>\
**Category:** Python Help\
**Created:** [November 14, 2024, 12:22am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099 "2024-11-14T00:22:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 14, 2024, 12:22am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/1 "2024-11-14T00:22:57Z")

</div>

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:

```python
# 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:

```python
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):

```python
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?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 14, 2024, 1:43am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/2 "2024-11-14T01:43:52Z")

</div>

In your post, the code has the line:

```python
# teststream.py

```

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

I ask because your output says:

```python
C:\Desktop\Temp_Directory>py teststreams.py

```

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

Are there 2 files with almost the same name?

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 14, 2024, 1:46am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/3 "2024-11-14T01:46:32Z")

</div>

Hello,

was a typo. it is `teststreams.py`.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 14, 2024, 1:49am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/4 "2024-11-14T01:49:54Z")

</div>

I suggest adding:

```python
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?

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 14, 2024, 1:55am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/5 "2024-11-14T01:55:46Z")

</div>

> [@MRAB](#):
>
> `print( __file__ )`

where specifically? I added it just above:

```python
def interact():

```

> [@MRAB](#):
>
> Are they _definitely_ running the same file?

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

> [@MRAB](#):
>
> Did you ever have lines that printed “pre if”, etc, in the file?

No, this is the very first time.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 14, 2024, 4:20am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/6 "2024-11-14T04:20:07Z")

</div>

**Update** :

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

```python
py new_file_with_original_contents.py

```

After running this new module, it worked as expected.

Very odd.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [November 14, 2024, 5:24am UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/7 "2024-11-14T05:24:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 14, 2024, 12:54pm UTC](https://discuss.python.org/t/odd-behaviour-when-running-scripts-from-the-cmd-line/71099/8 "2024-11-14T12:54:51Z")

</div>

> [@Rosuav](#):
>
> Check to see if there’s a file called `teststreams.pyc` or similar

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