Is there a simple way to run a Python script on Windows/Linux/OS X?
On the latter two, subprocess.Popen("/the/script.py")
works, but on Windows I get the following error:
Traceback (most recent call last): File "test_functional.py", line 91, in test_functional log = tvnamerifiy(tmp) File "test_functional.py", line 49, in tvnamerifiy stdout = PIPE File "C:\Python26\lib\subprocess.py", line 595, in __init__ errread, errwrite) File "C:\Python26\lib\subprocess.py", line 804, in _execute_child startupinfo) WindowsError: [Error 193] %1 is not a valid Win32 application
monkut's comment: The use case isn't clear. Why use subprocess to run a python script? Is there something preventing you from importing the script and calling the necessary function?
I was writing a quick script to test the overall functionality of a Python-command-line tool (to test it on various platforms). Basically it had to create a bunch of files in a temp folder, run the script on this and check the files were renamed correctly.
I could have imported the script and called the function, but since it relies on sys.argv
and uses sys.exit()
, I would have needed to do something like..
import sys import tvnamer sys.argv.append("-b", "/the/folder") try: tvnamer.main() except BaseException, errormsg: print type(errormsg)
Also, I wanted to capture the stdout and stderr for debugging incase something went wrong.
Of course a better way would be to write the script in more unit-testable way, but the script is basically "done" and I'm doing a final batch of testing before doing a "1.0" release (after which I'm going to do a rewrite/restructure, which will be far tidier and more testable)
Basically, it was much easier to simply run the script as a process, after finding the sys.executable
variable. I would have written it as a shell-script, but that wouldn't have been cross-platform. The final script can be found here
I write a simple script to check the subprocess module and I tested it on both Windows and Linux. The script works fine on Windows but not on Linux. The interpreter in python is used in 3 versions on both.
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!
To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.
Just found sys.executable
- the full path to the current Python executable, which can be used to run the script (instead of relying on the shbang, which obviously doesn't work on Windows)
import sys import subprocess theproc = subprocess.Popen([sys.executable, "myscript.py"]) theproc.communicate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With