1

I can run one program by typing: python enable_robot.py -e in the command line, but I want to run it from within another program.

In the other program, I imported subprocess and had subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e']), but I get an error message saying something about a callback.

If I comment out this line, the rest of my program works perfectly fine.

Any suggestions on how I could change this line to get my code to work or if I shouldn't be using subprocess at all?

zondo
  • 19,901
  • 8
  • 44
  • 83
madjab1
  • 11
  • 2

2 Answers2

0

If enable_robot.py requires user input, probably it wasn't meant to run from another python script. you might want to import it as a module: import enable_robot and run the functions you want to use from there.

If you want to stick to the subprocess, you can pass input with communicate:

p = subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e'])
p.communicate(input=b'whatever string\nnext line')

communicate documentation, example.

Community
  • 1
  • 1
fodma1
  • 3,485
  • 1
  • 29
  • 49
  • If I import it how would I run the function if it still requires a user to type -u or -e? The main method takes no parameters - it uses some type of argument parsing. I'm a newbie to Python, sorry if this is trivial stuff. – madjab1 Jul 29 '16 at 20:39
  • You might want to paste the content of that file then. Usually python programs are written in a way that they only rely on user input if they are run directly from command line `https://docs.python.org/3/library/__main__.html` but if you want to use other functions you can import those. – fodma1 Jul 29 '16 at 20:42
  • @leojim "main" has no special meaning in python, and usually it is a function, not a method. Are you coming from Java, by chance? – juanpa.arrivillaga Jul 29 '16 at 21:25
0

Your program enable_robot.py should meet the following requirements:

  • The first line is a path indicating what program is used to interpret the script. In this case, it is the python path.
  • Your script should be executable

A very simple example. We have two python scripts: called.py and caller.py

Usage: caller.py will execute called.py using subprocess.Popen()

File /tmp/called.py

#!/usr/bin/python
print("OK")

File /tmp/caller.py

#!/usr/bin/python
import subprocess
proc = subprocess.Popen(['/tmp/called.py'])

Make both executable:

chmod +x /tmp/caller.py
chmod +x /tmp/called.py

caller.py output:

$ /tmp/caller.py

$ OK

Jose Raul Barreras
  • 849
  • 1
  • 13
  • 19