Version 9 of exec path problems

Updated 2008-01-28 16:28:25 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. This sequence is documented in the exec man page: http://www.tcl.tk/man/tcl/TclCmd/exec.htm and differs among operating systems.

A potential gotcha occurs when the PATH used by the user's shell differs from that of Tcl. This is a problem for all programs that need to execute other programs and isn't restricted to interpreters such as Perl/Python/PHP let alone Tcl.

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.

E.g. With ImageMagick installed on Windows

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

#but this results in an error
tclsh> exec convert a.tiff 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.