0

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.

3
  • try to install python on your windows PC and add it at your environment.... stackoverflow.com/questions/1449494/… Commented May 21, 2014 at 12:48
  • See also stackoverflow.com/questions/11744181/… if you are using iPython as your console. Commented 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! Commented May 21, 2014 at 13:00

3 Answers 3

1

%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.

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

Comments

0

If you can make system calls, you can use:

import os
os.system("importer.py arguments_go_here")

Comments

0

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.

Comments

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.