0

I am using an outside program (.exe) written in Fortran in a Python script inside a loop by using

os.system("program.exe")

However, each time Python execute the outside program "program.exe", a black window pops up because of the Fortran program running. And I loop several times, so it can get annoying and impossible to work because of the black windows appearing and disappearing each time "program.exe" is executed.

So I wonder : Is it possible to execute "program.exe" in the background such that I don't see anymore the black windows poping up and I may use my computer for other tasks?

Thank you

EDIT : What have worked with me is

os.chdir("C:/Python_script") # change to the directory containing the program to execute in background
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(["program.exe"], startupinfo=startupinfo).wait()
Jonses
  • 57
  • 8
  • 1
    Does this answer your question? [How to start a background process in Python?](https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python) – Zoro Mar 25 '21 at 14:25
  • ...or maybe this one: https://stackoverflow.com/questions/1813872/running-a-process-in-pythonw-with-popen-without-a-console – CryptoFool Mar 25 '21 at 14:32
  • I have tried `subprocess.Popen(["cea2.exe"])` and also `subprocess.Popen(["rm","-r","cea2.exe"])` both don't work – Jonses Mar 25 '21 at 14:50

2 Answers2

0

You could use Popen from the subprocess module.

Here is an example taken from the answers of this question:

import subprocess

def launchWithoutConsole(command, args):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()

if __name__ == "__main__":
    # test with "pythonw.exe"
    launchWithoutConsole("d:\\bin\\gzip.exe", ["-d", "myfile.gz"])
user_na
  • 2,154
  • 1
  • 16
  • 36
  • This answer suggests that the question is a duplicate, and only copies information that already exists in the duplicate question you propose. You should vote to close this question as a duplicate. – CryptoFool Mar 25 '21 at 14:34
  • I have tried `subprocess` but it doesn't work. The program is not executed – Jonses Mar 25 '21 at 14:40
  • The problem is probably not Popen or subprocess. Try to start with a minimal example. – user_na Mar 25 '21 at 14:44
  • When I use `os.system("program.exe")` the Fortran program works. I have just replaced `os.system("program.exe")` by `subprocess.Popen("program.exe")` and it doesn't work. What do you think may be the cause of the fact it does not work ? – Jonses Mar 25 '21 at 15:12
-1

looks like you can find the answer here: How to start a background process in Python?

first answer in that thread.

urirot
  • 279
  • 1
  • 10
  • This is both a link-only answer and a duplicate, neither of which are valid answers. You you should vote to close the question as being a duplicate, providing this same link as the duplicate question. – CryptoFool Mar 25 '21 at 14:33
  • I have tried `os.spawnl(os.P_DETACH,"program.exe)`. I didn't work and it killed the kernel when I have tried to run in Python. I have tried also `subprocess` it doesn't work neither – Jonses Mar 25 '21 at 14:34