16

I am trying to run gphoto2 from python but, with no succes. It just returns command not found. gphoto is installed correctly, as in, the commands work fine in Terminal.

p = subprocess.Popen(['gphoto2'], shell=True, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, executable='/bin/bash')

for line in p.stdout.readlines():
    print line
p.wait()

/bin/bash: gphoto2: command not found

I know that there is something funny about the osx Terminal (app) but, my knowledge on osx is meager.

Any thoughts on this one?

EDIT

changed some of my code, other errors appear

p = subprocess.Popen(['gphoto2'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
    print line


    raise child_exception
OSError: [Errno 2] No such file or directory

EDIT

using full path '/opt/local/bin/gphoto2'

but if someone care to explain which shell to use or how to log in and be able to have the same functionality..?

Aduen
  • 421
  • 1
  • 4
  • 9

1 Answers1

12

When using shell = True, the first argument to subprocess.Popen should be a string, not a list:

p = subprocess.Popen('gphoto2', shell=True, ...)

However, using shell = True should be avoided if possible since it can be a security risk (see the Warning).

So instead use

p = subprocess.Popen(['gphoto2'], ...)

(When shell = False, or if the shell parameter is omitted, the first argument should be a list.)

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • thanks, refactored my code correctly know. Although it brought me no further. I have edited my post – Aduen Feb 29 '12 at 21:07
  • 3
    What happens if you use the full path to `gphoto2`? – unutbu Feb 29 '12 at 21:15
  • 11
    Try the command `which gphoto2` at the terminal to find the full path. – unutbu Feb 29 '12 at 22:14
  • 2
    I would avoid adding full path to the code... it is better to add to `os.environ['PATH']` as different runtime env might have different path – naoko Aug 02 '17 at 21:56