Updated 2005-08-23 18:55:56

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 for a pure-Tcl package called FooBar:

  • Create a package source file called FooBar.tcl
  • Populate that file with the package code, following the conventions in William Duquette's tutorial on 'Namespaces and Packages' [1].
  • Make sure that the 'FooBar.tcl' includes this command:
 package provide foobar 1.0

  • Create a pkgIndex.tcl file. You can use the pkg_mkIndex utility, but it is often easier to just write this by hand. Basically, you need to call [package ifneeded] to tell the package system about how to load your package. The load script is going to be a [source] command, using the variable $dir
 package ifneeded foobar "source [file join $dir FooBar.tcl]"

ramsan: I would change it to:
 package ifneeded foobar [list source [file join $dir FooBar.tcl]]

so as to avoid problems with spaces in $dir

  • Put both FooBar.tcl and pkgIndex.tcl into a directory (probably named foobar). If you put the foobar directory into your Tcl library path, then it will just work.
  • If you can't put foobar into the Tcl library path, then you can adjust the library path to point to its location. If your foobar directory is in /some/where, then set TCLLIBPATH to /some/where. You can also extend the search path in your scripts.
 lappend auto_path /some/where

Category Tutorial