Tcl语言中的set auto_path command

已有 1679 次阅读 2020-5-24 16:32 |个人分类:电脑技术|系统分类:科研笔记

在vmd使用Tcl语言的时候,我们经常需要调用一些额外的软件包,调用不恰当的时候经常会出现can not find package xxx的问题,这时候我们可以通过把需要调用包的绝对路径写在auto_path中来解决这一问题:



$auto_path, one of the variables managed by the interpreter, is a list of directories used by package to find packages, and also by auto_load when searching for commands.


If you need, with your application, to add a directory to the auto_path list, be certain to remember it is a list - so don't make the mistake of treating it as a single value.

Instead use something like

set auto_path [linsert $auto_path 0 /home/mystuff/someextension/unix]


lappend auto_path [file join $env(HOME) someextension unix]

Always use an absolute path, not a relative one.

Also make certain that auto_path (and env in the second case) are global references to the variables, either using global or $::

Tcl's init.tcl start up code sets auto_path as a global within an interpreter instance. As the distribution init.tcl for 8.3 says right at the top,

# The values on the path come from several locations:
# The environment variable TCLLIBPATH
# tcl_library, which is the directory containing this init.tcl script.
# tclInitScript.h searches around for the directory containing this
# init.tcl and defines tcl_library to that location before sourcing it.
# The parent directory of tcl_library. Adding the parent
# means that packages in peer directories will be found automatically.
# Also add the directory where the executable is located, plus ../lib
# relative to that path.
# tcl_pkgPath, which is set by the platform-specific initialization routines
#       On UNIX it is compiled in#       On Windows, it is not used
#       On Macintosh it is "Tool Command Language" in the Extensions folder

Two points to emphasize from the above:

  • Notice the rather baroque mixture of platform-dependence, generation-time, and run-time assignments.

  • As is customary in idiomatic Tcl, "the directory where the executable is located" refers to tclsh (and so on), rather than, for example, my_script.tcl. This directory is omitted in Tcl 8.4 and 8.5.

Question: is there a Wiki page that discusses how the whole Tcl start up, locate extension, load extension thing works? One of the toughest problems I encounter with Tcl is figuring out how to fix problems when an extension I think should load, doesn't load.

For instance, Tix is a Tk based extension. When one installs it, it places the .so in $exec-prefix/lib and its scripting in $exec-prefix/lib/tix8.2/ (or whatever version is being installed).

When this is a normal file system, and one does the

package require Tkpackage require Tix

the extension is found, loaded, and things work. When the same directory structure is used within a starkit, an error about not finding the Tix script code is generated.

Understanding the process better - and perhaps some debugging tools - would make fixing things like this easier.

下一篇:Pair_style lj/cut command


该博文允许注册用户评论 请点击登录 评论 (0 个评论)


Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2022-7-1 09:37

Powered by

Copyright © 2007- 中国科学报社