0

I want to make a script that opens a different console with a script attatched to it that runs when the other console opens so you don't have everything happening in the same console window, I've tried doing this but it didn't seem to work

function = """
the script I want to execute in the other window
"""
os.system('start /wait cmd /c python %s' % (function))
Rdimo
  • 378
  • 2
  • 14
  • If you want to put code as parameter, then you have to give -c flag to python. Like this: `python -c "import sys; print(sys.version)"` – h4z3 Sep 06 '21 at 15:37
  • 1
    Does this answer your question? [How can I open two consoles from a single script?](https://stackoverflow.com/questions/19479504/how-can-i-open-two-consoles-from-a-single-script) The usage of `os.system` to start the Windows command processor `cmd.exe` with option `/c` to close itself after finishing execution of the command line to use its internal command `start` with the option `/wait` to wait for the self-termination of a started second instance of `cmd.exe` with option `/c` to close after `python.exe` finished processing the script file is definitely not a good approach for this task. – Mofi Sep 06 '21 at 16:26
  • In fact on Windows the [subprocess module](https://docs.python.org/3/library/subprocess.html) is a Python wrapper library for the Windows kernel function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) used by `cmd.exe` and all other Windows executables to start another executable without or with [STARTUPINFO structure](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow). So the usage of `subprocess.Popen` is highly recommended for this task. – Mofi Sep 06 '21 at 16:33
  • The usage of [sys.executable](https://docs.python.org/3/library/sys.html#sys.executable) is also highly recommended as there is no guarantee that the Python script executed by `python.exe` is found by `cmd.exe` at all and if there is a `python.exe` found, that the same version of `python.exe` is found by `cmd.exe`. A user could have multiple versions of `python.exe` installed and used a specific version to run the Python script with your code, or the path of the directory with `python.exe` is not in `PATH` at all and the user used therefore the full qualified file name of `python.exe`. – Mofi Sep 06 '21 at 16:38

1 Answers1

1

I think your question is similar to this one. See if it helps.

How can I open two consoles from a single script

vic
  • 11
  • 2
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 06 '21 at 16:25