-3
print("test")
m = input("Name: ")
print(m)

I was getting ready to start programing. I opened cmd and ran my program and it opened a new cmd and printed out my code. Why is python opening a new cmd window to run my script unstead of using the cmd that was opened?

Also I recently updated python to python 3.10

Rob
  • 1
  • 1
  • 1
    How did you run your script? – MattDMo Nov 26 '22 at 01:31
  • I opened a cmd prompt, cd into desktop, then typed reponse.py C:\Users\Johm\Desktop>reponse.py The file is called reponse.py – Rob Nov 26 '22 at 01:34
  • What happens if you change to the Desktop folder, then run `py response.py`? Does that run it in the same window? – MattDMo Nov 26 '22 at 01:36
  • Try ‘python3 response.py‘. it looks like you may have a file association with py find that is running a command. – Ron Beyer Nov 26 '22 at 01:37
  • I am inside the desktop folder and it still opens a new window. the window is called ( C:\Program Files\WindowsApps\PythonSoftwareFoundation.python.3.10_3.102288.0_x64\python3.10.exe ) – Rob Nov 26 '22 at 01:40

2 Answers2

0

When you type the name of a file, and the program associated with the file type is a console application (like python.exe) then Windows will open a new console window to run the application. When the application closes, the command window will close automatically.

You can get around this by opening Python yourself in the current console:

python -m response.py
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Why the `-m` flag? OP's code isn't a module. – MattDMo Nov 26 '22 at 01:43
  • @MattDMo it's just a little cargo-cultism on my part. I've always seen the `-m` used in examples that I've seen and never stopped to ask what it does, just copied it blindly. Google turned up plenty of questions and answers here on Stack Overflow on the subject, and now I understand. Indeed you don't need it. – Mark Ransom Nov 26 '22 at 01:54
0

I'm very slow, so yes for whatever reason you now have to type python3 C:\Users\johm\Desktop>python3 reponse.py

This worked

Rob
  • 1
  • 1