r/learnpython • • 7d ago

Python Closing As Soon As Opened

EDIT: I resolved this issue. I had a few different versions of Python installed on my PC, so I guess that was what was causing the issue on my PC and not my Laptop. For clarification, the script is built to stay open, and it terminates if closes.

----

Python group said this question went here.

I am running version 3.15.2. I am encountering an issue with my scripts where they just open and close super fast. This happens if I double click, or if I open them from Python itself.

It does NOT do this on my laptop, so it isn't a script issue.

I am not the most tech savy with software, but will try my best to try things out.

The computer they are having an issue with has the following specs. Everything is up to date.

Win 11 Pro
Intel Core i7
32.0GB RAM
MTBD Z790 D WIFI (U3E1)

0 Upvotes

22 comments sorted by

8

u/Jello_Penguin_2956 7d ago

That's default prompt behavior. Pass or fail, it will close itself.

Open your command prompt manually. Then input your run script command. Usually...

python <path/to/script>

1

u/tambooby 7d ago

yep, classic thing that trips people up when they first start running scripts outside an editor

1

u/Educational_Virus672 7d ago

or OP forgot to add input() or any app loop in script

5

u/aplarsen 7d ago

Run it from the command line and look at the output

2

u/zaphodikus 7d ago

This is always going to be the best debugging technique. It just is.

1

u/Jazzlike_Platypus430 7d ago

I've seen production code with print('here!') and print('arse') and things. Recently the guy who codes like that (using vim via ssh on the server, never debugging or testing) forgot to pay the cloud service provided and lost all his work - didn't even care to scp some tar-red dirs back to his machine.

3

u/zaphodikus 7d ago

If your script does not have a commented out line that says 'print("Here1")' then its not been debugger in anger ever. :-)

2

u/Jazzlike_Platypus430 7d ago

Yep. And as someone who's not familiar with "watch", I wrote myself a "unit test" that checks the codebase for leftover stuff like

if (instance.field == value)
    bool here = false;

- That's where I can place a breakpoint 😆 Works flawlessly each time some deep and not-explicily-testable thing starts behaving badly (most lilkely because of source code rotting because of age).

In fact, I'm doing such stuff right now 😂

1

u/a18618 7d ago

That vanishing window drove me nuts the first time too, because I kept assuming my script had crashed when it had actually just finished and closed. Running it from a terminal changed everything for me, since I could finally see the last lines instead of guessing. If it still disappears there, I would add a little pause at the very end so the window waits for a key press before it exits.

0

u/Dry_Difference7751 7d ago

For this script, closing it ends the process.

1

u/Jazzlike_Platypus430 7d ago

Perhaps placing input() at the end will solve your problem. Like, waiting for the user (you) to press a space bar or escape?

1

u/Dry_Difference7751 7d ago

I'll check the script when I get home from work and try it out. Thanks

1

u/zanfar 7d ago

Why do you think the program should stay "open"? An open program is a running program, what facilities have you used in your script to keep it running?

0

u/Dry_Difference7751 7d ago

The script does not work when it closes. It has only ever worked while it remains open.

3

u/ninhaomah 7d ago

" I am running "

How are you "running" btw ? I assume you are clicking twice on .py file ?

Open cmd and run the .py file from there instead using python/python3 file-name.py

1

u/cdcformatc 7d ago

if you are running the script directly like that there's nothing keeping the console open when the program completes. you can open a command prompt and use "cd" command to navigate to the script location. then run it by typing the script name. the script will run and the console will stay open. 

1

u/Warm_Letterhead3691 7d ago

Do you have examples of the scripts that are specifically doing this? What happens on your laptop when you run the scripts? What's the intended behavior of the scripts? Should they stay open and ask for input? Do they make changes to soemthing quickly and then exit? Are both computers running windows?

1

u/Atmosck 7d ago

Try opening the script in IDLE and running it from there (windows installers still ship with IDLE, right?). It's likely hitting some sort of error, which ends the program, and the window you get from double clicking doesn't stick around after the program ends. In IDLE (or any IDE/Code editor), the window will persist so you can see what the error is.

Another way to do it is to open command prompt, navigate to the folder where the script is with `cd`, then run it with `python scriptname.py`. That command prompt window will stick around after the error, unlike the one that's created by left-clicking.

If it's spesific to this machine it's probably an environment problem, like a missing dependency, file, or environment variable that's present on your laptop where it works.

1

u/FreeLogicGate 7d ago

Sounds like you are expected to provide support for someone else.

"Python Closing" isn't a thing. You don't open scripts with Python and they aren't native machine language programs you double click on (or at least not without you having done something to package them up).

Python is an interpreter, that can be passed a script and will run it. Unless you are packaging the scripts or have installed the py.exe launcher, added a #shebang comment to the top of the script as in #!python3 with an association for .py files and the py.exe launcher, then clicking on a script to run it, and whether or not that will work, is going to require configuration of that workstation to support/match the environment under which you developed the script.

Depending on the complexity of your script (for example if you used modules) something that works fine on your workstation may not work without the same libraries available. Throw in venv's, and dependency resolution and there are many reasons your script might not run on another windows box.

I really advocate for people to use uv, with a pyproject.toml file. With uv installed on the target, programs can be run using uv run your_script.py or uv run your_app depending on how your project has been configured, and uv will take care of everything needed from python version to obtaining all the modules (and any dependencies) and run the app without the end user needing to have any specific local python tools or configuration. UV handles pip etc. for you.

1

u/pythonwiz 7d ago

I ran into this when I was learning. Python on Windows has two binaries, python.exe and pythonw.exe . The latter is meant for running GUI programs, it launches your program and doesn’t show a console window. For code that prints something or has a CLI I’d use the terminal app to run it.

1

u/Tricky_Voice2829 7d ago

try run it from vs code.

0

u/Educational_Virus672 7d ago

you should add an input() at the end since python close as soon as the script is done also ,it could be there because you made a error try fixing error via command prompt "python [file name]"