1

I'm starting out my adventure with Python, so I apologise if there are inaccuracies in my question. I have written a simple module with one command of:

print("test")

After saving the file to a .py extention I was trying to run it within a cmd terminal. Unfortunatelly a sub-shell of sorts pops out and immediately disappears. I would like the script to open within the same terminal, not open in a new one/sub-shell. What could be the cause of such behaviour? Thank you!

ReeD
  • 11
  • 3
  • 1
    So just how *do* you `run it within a cmd terminal`? – quamrana Dec 26 '20 at 17:45
  • Let say I have saved the file on my desktop. I'm opening the terminal and navigating my way to Desktop directory, typing in "test.py" and running the module – ReeD Dec 26 '20 at 17:51
  • Does [How to stop Python closing immediately when executed in Microsoft Windows](https://stackoverflow.com/questions/12375173/how-to-stop-python-closing-immediately-when-executed-in-microsoft-windows) answer your question? Or [Window closes immediately after running program](https://stackoverflow.com/questions/25005943/window-closes-immediately-after-running-program)? – wwii Dec 26 '20 at 17:52
  • Ohters searching with variations of `python run script window pops up and closes site:stackoverflow.com` – wwii Dec 26 '20 at 17:55
  • @wwii, partially, but the threads came in handy anyway. Thank you! – ReeD Dec 26 '20 at 17:56

2 Answers2

0

You mentioned cmd terminal so I would assume you mean windows command prompt.

I would ask that you clarify which command you are using to execute the python file.

Consider trying:

python filename.py

It should run the program on same prompt.

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • I haven't been using any commands to execute the python file. Your sugestion worked! Thank you! The cause of my problem was not running the file through an interpreter, but simply trying to display its contents. Is that correct? – ReeD Dec 26 '20 at 17:54
  • yes that's the default behavior – Shubham Takankhar Dec 26 '20 at 18:00
  • No, if you want to display the contents of a file you could enter `type test.py` – quamrana Dec 26 '20 at 18:03
0

In windows when you just type the name of a file, it will try to find an application to run the file for you based on the extention. For example, typing in file.txt might cause NotePad.exe to run. Similarly, typing test.py may cause windows to find a python interpreter to run your script.

These may or may not run in their own windows. However, if you enter python test.py, then windows will be looking for python, and once found to be an application already, will run it with the whole command line.

quamrana
  • 37,849
  • 12
  • 53
  • 71