0

I make program in python to open some executable file given by path. I use:

os.startfile(path)

to start program. When i run script.py in IDLE it works fine, but when i make .exe file with pyinstaller when i run it that program i want to open starts to open and closes almost immediately. I tried with different functions like subprocess.Popen(path) and it does the same thing, open and close program after 1 second. Can someone help me? Is there a problem in python functions or in pyinstaller or even windows 10?

1 Answers1

0

The problem is that your code most likely just runs and then the window closes.

You can add import time to your imports and time.sleep(x) or something to keep the window open for x seconds after you run

Read all the way through before doing anything!

Ok so try this:

Put this in a python file called start.py:

import os
import subprocess

#py_command - whatever opens python in cmd
#path - must be full path of file you want to call

def StartPythonScript(path, py_command):    
    command = 'start cmd /k ' + py_command + " " + path
    subprocess.run(command, shell=True)

Now, take the code from file you are calling from the exe, let's say its name is run.py, and put it in a different file, say code.py.

Make sure start.py is in the same folder as run.py

In run.py, put this:

import start

cmd = "py" #change this to whatever opens python in cmd
path = "code.py" #change to the full path of code.py
start.StartPythonScript(path, cmd)

So when you click on the exe, it opens run.py which tells start.py to open code.py, which contains your program.

You can actually merge start.py and run.py, but you can reuse start.py if they are seperate.

OR...

Add import os to your program that is called by the exe. Add os.system("pause") to the end of your program.

I'm not sure if this will work on an infinitely running program...but try this first to save time.

More info:

How to stop Python closing immediately when executed in Microsoft Windows

Good luck!

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37
  • No, my program works infinitely. When I want to run other program it continues to work –  May 02 '19 at 21:28