The python script I would use (source code here) would parse some arguments when called from the command line. However, I have no access to the Windows command prompt (cmd.exe) in my environment. Can I call the same script from within a Python console? I would rather not rewrite the script itself.
-
try to install python on your windows PC and add it at your environment.... stackoverflow.com/questions/1449494/…angel– angel2014-05-21 12:48:20 +00:00Commented May 21, 2014 at 12:48
-
See also stackoverflow.com/questions/11744181/… if you are using iPython as your console.Wolf– Wolf2014-05-21 12:51:55 +00:00Commented May 21, 2014 at 12:51
-
@Wolf Simply '%run' does the trick, thanks! Are there any pitfalls to that? Otherwise feel free to write up a a quick answer about it, I am happy to give you credit!László– László2014-05-21 13:00:00 +00:00Commented May 21, 2014 at 13:00
3 Answers
%run is a magic in IPython that runs a named file inside IPython as a program almost exactly like running that file from the shell. Quoting from %run? referring to %run file args:
This is similar to running at a system prompt python file args,
but with the advantage of giving you IPython's tracebacks, and of
loading all variables into your interactive namespace for further use
(unless -p is used, see below). (end quote)
The only downside is that the file to be run must be in the current working directory or somewhere along the PYTHONPATH. %run won't search $PATH.
%run takes several options which you can learn about from %run?. For instance: -p to run under the profiler.
Comments
You want to spawn a new subprocess.
There's a module for that: subprocess
Examples:
Basic:
import sys
from subprocess import Popen
p = Popen(sys.executable, "C:\test.py")
Getting the subprocess's output:
import sys
from subprocess import Popen, PIPE
p = Popen(sys.executable, "C:\test.py", stdout=PIPE)
stdout = p.stdout
print stdout.read()
See the subprocess API Documentation for more details.