Version 11 of exec path problems

Updated 2008-01-28 17:04:19 by evilson

An exec Gotcha

Tcl looks for the command to be exec'ed in a sequence of directories part of which may be described by the PATH. Exactly what name exec searches for and the sequence of directories searched differs among operating systems and is nicely documented in the exec man page: http://www.tcl.tk/man/tcl/TclCmd/exec.htm

A potential gotcha occurs when the PATH used by the user's shell differs from that of Tcl. They may well look in the same directories but may not do so in the same sequence. This is a problem for all programs that need to execute external programs and isn't restricted to interpreters such as Tcl/Perl/Python/PHP.

When this happens calling the command from the shell or commandline (say Dos) works but execing it from Tcl either fails completely or calls a different program with the same name. This is not an uncommon problem as shown by questions on comp.lang.tcl.

E.g. With ImageMagick installed on Windows

# this works
dos> convert a.tif a.jpg

#but this results in an error
tclsh> exec convert a.tif a.jpg
  Invalid Parameter - a.jpg
  Child process exited abnormally 

What's happening here is that Tcl is invoking C:\WINDOWS\system32\convert.exe while dos invokes the convert.exe installed in the ImageMagick directory.

The moral of the story is not to assume that if a command works from the commandline, then it will also be execed successfully by Tcl or any other interpreter or program.

A solution that will always work is to type the full path to the external program.

So for the e.g. above, assuming that ImageMagick is installed in C:\Program Files: this will work:

tclsh> exec {C:/Program Files/ImageMagick-6.3.8-Q16/convert} a.tif a.jpg