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))
4
  • 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)" Commented Sep 6, 2021 at 15:37
  • 1
    Does this answer your question? 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. Commented Sep 6, 2021 at 16:26
  • In fact on Windows the subprocess module is a Python wrapper library for the Windows kernel function CreateProcess used by cmd.exe and all other Windows executables to start another executable without or with STARTUPINFO structure. So the usage of subprocess.Popen is highly recommended for this task. Commented Sep 6, 2021 at 16:33
  • The usage of 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. Commented Sep 6, 2021 at 16:38

1 Answer 1

1

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

How can I open two consoles from a single script

Sign up to request clarification or add additional context in comments.

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.