7
$\begingroup$

I am using a publicly available Mathematica package on this page and is loaded in Mathematica by the command

<< EccentricIMR`;

How do I unload this package? I don't want to use 'Quit' or 'Exit' since I lose all the definitions.

Why do I want to do this? I have two very similar packages with a lot of functions whose names coincide when both the packages are loaded at once. I plan to unload one before I load the other.

$\endgroup$
3

3 Answers 3

14
$\begingroup$

Depending on your definition of "unload", it may be enough to remove the package's contexts from $ContextPath.

The symbols will still exist in your kernel, but you'd only run into them by using their full names including the context.

$\endgroup$
9
$\begingroup$

Easiest and most secure method is restarting the kernel with Exit.

You can also try

names = Join[ Names["EccentricIMR`*"] , Names["EccentricIMR`*`*"];
ClearAll /@ names
Remove /@ names

But there is not guarantee that this removes all symbols. E.g., symbols in deeper hidden contexts are neither cleared nor removed this way. Also Protected symbols cannot be removed this way.

$\endgroup$
1
  • 1
    $\begingroup$ Exit and Quit will erase all the definitions. I don't want that. $\endgroup$ Jul 15, 2019 at 1:48
-1
$\begingroup$

I found a hack. The purpose of unloading the package was to avoid conflicts. There is a way to do that. Every package file begins with the statement of the Context. See here.

BeginPackage["square`"];

Here, 'square' is the context. Since we have two similar packages containing functions with the common names, what we first have to do is to keep the context names different. For example, if the first package file begins with

BeginPackage["square`"];

then the second package must begin differently, say

BeginPackage["square2`"];

So now, we have created two different contexts which will help us delineate between the two packages. Let's say there is a function with the common name 'func' in both the packages. Here is how I will use func

square`func[....]
square2`func[....]

The first (second) line invokes the func function from the first (second) package.

$\endgroup$
3
  • 7
    $\begingroup$ Erm. Yes, that is the way how contexts work; definitely not a hack. Btw., you missed to tell us that you have control over the package source. $\endgroup$ Jul 15, 2019 at 3:50
  • 5
    $\begingroup$ Sorry but this has nothing to do with 'unloading'. This could be the answer to the question how packages/contexts work. Or did I miss the point? $\endgroup$
    – Kuba
    Jul 15, 2019 at 6:39
  • $\begingroup$ Yeah, I did not unload the package. But it serves my purpose. $\endgroup$ Jul 15, 2019 at 7:09

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.