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.
- How does Mathematica know this package has been loaded already?
- 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.)
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 thatGet
is not invoked the second time by usingOn[Get]
.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 toNeeds
will do nothing except returning your context back on the$ContextPath
.
精彩评论