-1

I want to open a new cmd and automatically run a method from my original script on the new cmd (while the old cmd is open).

My code so far:

def hello():
    print(say_hello)

say_hello = 'hello'
os.system("start cmd.exe")

Now how do I continue from here? I was able to open a new cmd with the line os.system("start cmd.exe") But dont know how to call the hello() method on the new cmd instantly.

Thanks in advance!

Razzer
  • 492
  • 1
  • 4
  • 29
Santos
  • 39
  • 5
  • 2
    You can't do that. If you start a new shell, you would have to run Python again and start your program from the beginning. That can be done, of course, but it won't have any connection to the original script. – Tim Roberts Aug 26 '23 at 17:30
  • 2
    Having said that, it IS possible, by using Win32 APIs in `win32console`, to open a second console from your program and start outputting things there. Is that what you're after? It's a pain, but it can be done. – Tim Roberts Aug 26 '23 at 17:31
  • 1
    Is it possible to automatically open a new cmd and automatically run script x? Like os.system('start cmd.exe') and os.system('python x.py') combined? – Santos Aug 26 '23 at 17:46
  • 2
    Absolutely. `cmd.exe` accepts parameters. Do `cmd /?` to see the options. `os.system("start cmd.exe /C python x.py")` If you want the window to stay open after the script exits, use `/K` insteadl – Tim Roberts Aug 26 '23 at 17:50
  • 3
    [os.system](https://docs.python.org/3/library/os.html#os.system) is deprecated since more than 10 years. It should not be used anymore in newly written *Python* scripts. The reason is quite simple. This function calls the [system function](https://learn.microsoft.com/en-us/cpp/c-language/system-function) which on Windows calls the the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) for execution of `%ComSpec%` with option `/c` and the string passed to `os.system()` appended as arguments. – Mofi Aug 26 '23 at 18:01
  • 2
    `%ComSpec%` references the value of the environment variable `ComSpec` defined by Windows default with `%SystemRoot%\system32\cmd.exe` which expands usually to `C:\Windows\system32\cmd.exe`. So, there is called `CreateProcess` for running the *Windows Command Processor* for execution of a command line specified after the __CMD__ option `/c` to run the command line and then close itself. `cmd.exe` uses also `CreateProcess` to run executables without or with using its internal command `start`. – Mofi Aug 26 '23 at 18:05
  • 1
    The internal command `start` gives the user of the *Windows Command Processor* a very little bit control over how `cmd.exe` calls `CreateProcess` for running an executable. A reader of Microsoft's `CreateProcess` documentation and of the usage help of command `start` output on running `start /?` in a command prompt window notices quickly which `start` options changes values passed to `CreateProcess` with using the [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure too. – Mofi Aug 26 '23 at 18:08
  • 1
    There is something better for *Python* 3.x programmers - the [subprocess module](https://docs.python.org/3/library/subprocess.html). That module is on Windows a *Python* wrapper module for calling `CreateProcess` directly without or with a `STARTUPINFO` structure. The usage of `subprocess.Popen()` can be used by a *Python* programmer to start any executable in any wanted way including starting a Windows console application running parallel to `python.exe` interpreting the *Python* script with opening a new console window and even size and position of the window can be defined in Python script. – Mofi Aug 26 '23 at 18:11
  • 1
    A *Python* programmer should just read the entire Microsoft documentation for `CreateProcess` and the `STARTUPINFO` structure and the documentation about the [Process Creation Flags](https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags) (and keep the three pages opened with three separate tabs in web browser). Then is opened the *Python* documentation for the `subprocess` module and most of the information provided here makes suddenly sense. It should be finally no problem anymore to write the few lines in Python script to run another executable as wanted. – Mofi Aug 26 '23 at 18:16
  • 1
    PS: Please read about the [XY problem](https://xyproblem.info/) as well. It looks like you code a *Python* script interpreted by `python.exe` which should run another `python.exe` for interpreting another *Python* script. Why not running both *Python* scripts by same `python.exe` using a thread for the second script? Why is the code of the other script not embedded in your script? How do you want to make sure that the other `python.exe` is of same version as the `python.exe` interpreting your script? – Mofi Aug 26 '23 at 18:22
  • 1
    A user can have multiple *Python* versions installed and executed a specific version of *Python* for interpreting your script as it is perhaps not compatible in future anymore with newest *Python* version. How do you make sure that the current working directory as defined by the process starting `python.exe` for interpreting your script by calling `CreateProcess` with whatever pointer value for `lpCurrentDirectory` contains the other *Python* script file referenced with no path which means with a path to current directory which can be any directory? – Mofi Aug 26 '23 at 18:25
  • 1
    Well, the code in the answer on [How do I get the full path of the current file's directory?](https://stackoverflow.com/a/3430395/3074564) could be used to get the absolute directory path of the directory containing your *Python* script and concatenate that path with the names of files in same directory. [sys.executable](https://docs.python.org/3/library/sys.html#sys.executable) could be used to get the fully qualified file name of `python.exe` interpreting your script which can be used to start one more instance of this executable with the other script file name as argument. – Mofi Aug 26 '23 at 18:28
  • 2
    You see, lots of documentations should be read and lots of information should be considered on writing a *Python* script which has dependencies on other executables and files for writing a *Python* script working by good designed and written code as considering every aspect of execution environment and not working just by chance depending on an execution environment defined by other processes with their code and how the user makes use of your *Python* script. It is not a waste of time thinking first if the task could be done different avoiding dependencies on other executables and files. – Mofi Aug 26 '23 at 18:32

1 Answers1

1

To open a new command prompt window and automatically run a method from your original script, you can use the subprocess module in Python.

import subprocess

def hello():
    print(say_hello)

say_hello = 'hello'

# Open a new command prompt window and run the hello() method
subprocess.Popen(["cmd.exe", "/C", "python -c \"from __main__ import hello; hello()\""])

We're using .popen() to open a new command prompt window and run a Python command inside it. The /C option is used to close the command prompt window after the command is run.

The python command uses the -c option to specify a Python command to run. After that, we import the hello method that you created. __main__ is the script, that is currently running.

Razzer
  • 492
  • 1
  • 4
  • 29