1
$\begingroup$

I'm just beginning with packages, so this is definitely a beginner's question. Suppose that I do this in a notebook:

BeginPackage["David`"]
$Context
$ContextPath
EndPackage[]
$Context
$ContextPath

The output is:

 "David`"
 "David`"
 {"David`", "System`"}
 "Global`"
 {"David`", "WolframAlphaClient`", "IconizeLoader`", "HTTPHandlingLoader`", 
  "StreamingLoader`", "InterpreterLoader`", "CloudObjectLoader`", 
  "PacletManager`", "System`", "Global`"}

Note that "David`" is the first on the $ContextPath. Now, my question is, how to clear. I know one way is to simply quit the kernel with Quit. I also did a little searching and tried this:

$ContextPath = DeleteCases[$ContextPath, "David`"]

The output is:

{"WolframAlphaClient`", "IconizeLoader`", "HTTPHandlingLoader`", "StreamingLoader`", "InterpreterLoader`", "CloudObjectLoader`", "PacletManager`", "System`", "Global`"}

So "David`" has been removed from the $ContextPath, so any functions or symbols created in the package (I did none) will not be found unless I put in their "long" name (such as David`total). Am I correct?

But I am thinking this might not be enough, so I'd like to ask my colleagues "How do you clear a package?"

$\endgroup$
3
  • 1
    $\begingroup$ There is no way to fully 'clear a package'. Once it's loaded, it stays loaded until you restart the kernel. What exactly are you trying to achieve? $\endgroup$
    – Szabolcs
    Jul 1, 2017 at 5:33
  • 1
    $\begingroup$ The most sure way to clear a package is to quit the kernel. There used to be cleanSlate package to fully clear package without restarting kernel, but I do not know if it is still supported on newer versions. $\endgroup$
    – Nasser
    Jul 1, 2017 at 7:10
  • 1
    $\begingroup$ To be clearer, returning the kernel to the exact state before loading the package is not realistic. But most of the aspects of unloading that are desired in practice (such as not having the package context in the context path) are achievable. However, no method is going to be completely robust, as many details depend on what the particular package does. $\endgroup$
    – Szabolcs
    Jul 1, 2017 at 13:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.