# How to unload a Mathematica package?

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.

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.

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.

• Exit and Quit will erase all the definitions. I don't want that. – Sashwat Tanay Jul 15 at 1:48

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

squarefunc[....]
square2func[....]
`

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

• 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. – Henrik Schumacher Jul 15 at 3:50
• 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? – Kuba Jul 15 at 6:39
• Yeah, I did not unload the package. But it serves my purpose. – Sashwat Tanay Jul 15 at 7:09