Version 10 of Just getting a package found already

Updated 2004-09-03 23:36:41 by GPS

I struggled for a couple of hours on just the basics of creating a package and getting it found. No matter what I tried, I kept getting the annoying and uninformative error:

    Error in startup script: can't find package FooBar 1.0

Here is the bare minimum of what I had to do to create a new package called FooBar and have it found:

  • Create a package file called FooBar.tcl.
  • Populate that file with the package code, following the conventions in William Duquette's tutorial on 'Namespaces and Packages' [L1 ].
  • Create a pkgIndex.tcl file so that "package require" works by calling pkg_mkIndex from inside a tclsh session. The call to tclsh8.4 below should be changed to the location of where your tclsh executable resides, but this worked on Tcl 8.4.1. Change directories into the directory where FooBar.tcl resides, and do something like this:
       echo pkg_mkIndex -verbose . FooBar.tcl | tclsh8.4
  • Make sure TCLLIBPATH is exported in the wrapper shell script that invokes tclsh on your Tcl script. I used a wrapper script because I wanted to change the environment to point to a more recent build of Tcl and Tk that resides in a different directory than in their standard locations. See Invoking tclsh or wish from Bourne Shell for an example wrapper script.
  • Inside the script that needs the FooBar package, have it require the package, where the 1.0 corresponds to the package provide command that should be inside the FooBar.tcl file:
       package require FooBar 1.0
  • Inside the FooBar.tcl file, have it provide the package:
       package provide FooBar 1.0

George Peter Staplin: You also may find that you will need to lappend ::auto_path /some/path; In some cases you may possibly have multiple versions installed, thus set ::auto_path linsert $::auto_path 0 /the/path; may be required to prevent the first version installed from being used.