Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I spawn new shells to run Python scripts from a base Python script?

I have successfully run several Python scripts, calling them from a base script using the subprocess module:

subprocess.popen([sys.executable, 'script.py'], shell=True) 

However, each of these scripts executes some simulations (.exe files from a C++ application) that generate some output to the shell. All these outputs are written to the base shell from where I've launched those scripts. I'd like to generate a new shell for each script. I've tried to generate new shells using the shell=True attribute when calling subprocess.call (also tried with popen), but it doesn't work.

How do I get a new shell for each process generated with the subprocess.call?

I was reading the documentation about stdin and stdout as suggested by Spencer and found a flag the solved the problem: subprocess.CREATE_NEW_CONSOLE. Maybe redirecting the pipes does the job too, but this seems to be the simplest solution (at least for this specific problem). I've just tested it and worked perfectly:

subprocess.popen([sys.executable, 'script.py'], creationflags = subprocess.CREATE_NEW_CONSOLE) 
like image 903
emiguel Avatar asked Jun 24 '11 14:06

emiguel


People also ask

How do I run a python script from another script?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.

How do I run a python script from the shell?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

Which commands will execute a python script called script in bash?

The -s option to sh (and to bash ) tells the shell to execute the shell script arriving over the standard input stream. The script then starts python - , which tells Python to run whatever comes in over the standard input stream.


2 Answers

To open in a different console, do (tested on Windows 7 / Python 3):

from sys import executable from subprocess import Popen, CREATE_NEW_CONSOLE  Popen([executable, 'script.py'], creationflags=CREATE_NEW_CONSOLE)  input('Enter to exit from this launcher script...') 
like image 142
Pedro Vagner Avatar answered Oct 05 '22 23:10

Pedro Vagner


Popen already generates a sub process to handle things. You just need to redirect the output pipes. Look at the subprocess documentation, specifically the section on popen stdin, stdout and stderr redirection.

If you don't redirect these pipes, it inherits them from the parent. Just be careful about deadlocking your processes.

You wanted additional windows for each subprocess. This is handled as well. Look at the startupinfo section of subprocess. It explains what options to set on windows to spawn a new terminal for each subprocess. Note that it requires the use of the shell=True option.

like image 22
Spencer Rathbun Avatar answered Oct 06 '22 01:10

Spencer Rathbun