开发者

How to prevent package from reloading?

Consider the following

Needs["Combinatorica`"];
$ContextPath = DeleteCases[$ContextPath, "Combinatorica`"];

When I execute it the second time, it's 10 times faster and it doesn't print the compatibility warning.

  1. How does Mathematica know this package has been loaded already?
  2. What's a good way to avoid reloading the package when it's been loaded and removed from $ContextPath?

I'm relying on Combinatorica for some graph algorithms, but I need to redefine Element every time it is开发者_JS百科 loaded, so I'm trying to keep reloading to minimum


From the usage message for Needs:

Needs["context`"] loads an appropriate file if the specified context is not already in $Packages.

Are you sharing your code with anyone else, or could you just edit your copy of Combinatorica to not add the problematic definition for Element? (Should probably verify that Combinatorica isn't using it internally in a way that would cause problems.)


  1. You probably want to look at $Packages variable. The first time you load, your context gets appended to it. The second time, it is not loaded at all, since it is already in $Packages, so nothing is happening thesecond time (apart from bringing the context back on the $ContextPath, but you delete it anyway). You can verify that Get is not invoked the second time by using On[Get].

  2. Since there is no actual reloading happening the second time, you don't have to do anything. But this also means that if you want to really reload the package, you have to first delete its context from $Packages, otherwise the call to Needs will do nothing except returning your context back on the $ContextPath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜