Anand
Anand

Reputation: 1122

tcl - how to find the path of package loaded?

In tcl how does one find out the path of the package loaded?

% tclsh
% package require csv

I want to find out the path from which csv was loaded.

In python, one can find the path of a module using

>>> import os
>>> print os.__file__
'/a/b/python2.2.1/linux26_x86_64/lib/python2.2/os.pyc'

I am looking for a similar command in tcl

Upvotes: 9

Views: 20457

Answers (4)

Andreas Otto
Andreas Otto

Reputation: 329

as mentioned before, tcl is weak on housekeeping… I use a simple trick to do this…

package provide lib_filter 1.0

namespace eval lib_filter {
  variable self [info script]
  ....
}

The variable lib_filter::self has the path and can easily be tested on. this works for script and also for C but have to be added by the programmer.

Upvotes: 0

My Name
My Name

Reputation: 166

This manual addresses auto_path and other loadable library variables: https://www.systutorials.com/docs/linux/man/n-auto_path/ New or missing loadable package search directory can be added within tclsh: lappend auto_path /new_directoty

Upvotes: 0

syox
syox

Reputation: 71

For Tcl packages you can view list of all loadedable path dirs by command:

join $::auto_path \n

Upvotes: 7

kostix
kostix

Reputation: 55443

It's not that simple: a package in Tcl appears to be a more abstract thing than that in Python.

First, there are two kinds of packages: "classic" and "modules" which have different underlying mechanisms for finding what to load in response to the package require ... command.

Next, both kinds of packages are able to do whatever they wish to provide their functionality. It means they can be (but not limited to):

  • Pure Tcl packages, source'ing just one Tcl file or any number of files.
  • Packages implemented in C or another compiled language, which are in the form of dynamic library which gets loaded when the package is required.
  • A combination of the above, when there's a C library and a layer of Tcl code around it (usually providing helper/convenience commands).

Hence the question per se has little sense as only modules are represented by exactly one self-contained file but "classic" packages are free to implement themselves as they see fit.

On the other hand, each package normally provides (using one way or another) certain information to the package subsystem which can be retreived (and parsed) using the package ifneeded command. For instance, on my Windows system with ActiveState Tcl 8.5.x, I have:

% package require csv
0.7.2
% package ifneeded csv 0.7.2
package provide csv 0.7.2;source -encoding utf-8 {C:/Program Files/Tcl/lib/teapot/package/tcl/teapot/tcl8/8.3/csv-0.7.2.tm}

Note that what package ifneeded returns is just a Tcl script which is meant to be evaluated to get the package loaded, so parsing of this information is bound to be inherently ad-hoc and fragile.

Upvotes: 21

Related Questions