19

I know that this has been discussed here (How do I clear all user defined symbols?), but my case is somewhat different. How does one unload packages during runtime that were loaded with the start of Mathematica? Occasionally, for deployment reasons, I would like to revert my system to the default state, to check whether a function works in that environment as well. I have a package that starts when Mathematica starts, and it contains some modified system symbols too (e.g. extended usage messages for options). Should I include CleanSlate`​ in the same init.m file before loading my own package and then later refer to it when I want to return to the default Mathematica state? Would it revert modified System` context symbols too?

At the moment I manually have to edit the init.m to remove package loading and then the kernel must be restarted. This is quite tedious.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
István Zachar
  • 47,032
  • 20
  • 143
  • 291

2 Answers2

20

If you want to revert the entire system to some state, then CleanSlate` may be the best option. If you want to unload a few specific packages though, you can use my package PackageManipulations`, available here. It has a function PackageRemove, which does exactly that. It has an accompanying notebook with explanations. Some additional notes on it are in this and this answers. If you want to just clear all the package and sub/package symbols but not Remove them, you can use functions PackageClear and PackageClearComplete from the same package (a disclaimer : the package may contain bugs, although I used it successfully many times)

Note that removing all symbols in the package (and subsequently removing its context) - which is what PackageRemove does and what you seem to ask for - may be unsafe if other packages use some of those symbols, since those symbols in the definitions of symbols in dependent packages would turn to Removed[symbs] and won't be usable any more, in a sutble way. To check that there are no such dependencies, you can use another package I wrote, PackageSymbolsDependencies`, available from the same place (it also has a notebook with explanations and examples).

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
9

The cleanest possible kernel you can get is starting a kernel only (no front end) with the -noinit option.

math -noinit

The front end will evaluate a lot of code when it connects to the kernel. -noinit prevents the kernel from loading initialization files (init.m). I don't believe you can get to a purer state than this unless you start surgically removing components that are practically built in (Remove, Clear, etc. applied to pre-defined symbols).

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    +1 I have a kernel configuration called "SafeMode" that starts with -noinit. I use it whenever testing code for posting or debugging. – Mr.Wizard Feb 08 '12 at 22:28
  • @Mr.Wizard How did you set up the SafeMode kernel? When I do that and choose Evaluation > Start kernel > SafeMode from under the frontend, no kenel is loaded. – István Zachar Mar 08 '14 at 10:10
  • @István I use Evaluation > Notebook's Kernel > SafeMode. Please try that and report the result. – Mr.Wizard Mar 08 '14 at 21:44
  • @Mr.Wizard Ok, so now I figured how to configure and start a kernel, but even if I assign SafeMode to the notebook, and launch it (with correctly passing -noinit), and no other kernel is running than SafeMode ... it still loads my ...\Kernel\init.m. – István Zachar Mar 08 '14 at 22:45
  • @István I don't know why that would be; perhaps it is version dependent. I suggest you post a Question on the topic. – Mr.Wizard Mar 09 '14 at 08:58
  • @Mr.Wizard I can't configure a safemode kernel either. Should I add "-noinit" at the end of "argument to MLopen"? but it will still loading init.m as Zachar said. I tried to add "-noinit" to "shell command ", then this kernel just can't start. – matheorem Oct 13 '15 at 13:01
  • @matheorem This is what my configuration looks like under Windows. The name "SafeMode" is arbitrary. http://i.stack.imgur.com/ED71F.png – Mr.Wizard Oct 14 '15 at 22:27
  • @Mr.Wizard Thank you, this works! – matheorem Oct 15 '15 at 09:09
  • @matheorem Glad I could help. :-) – Mr.Wizard Oct 15 '15 at 11:46
  • @István Incidentally did you ever get this working? If not would you try explicit configuration shown in http://i.stack.imgur.com/ED71F.png as well? – Mr.Wizard Oct 15 '15 at 11:47
  • 1
    @Mr.Wizard In v10 I can load a safe kernel with the following changes according to the recent renaming frenzy: MathKernel > WolframKernel, -mathlink > -wstp. I also figured out (just now) why my init.m was loaded, irregardless of the kernel arguments used: I've had my init.m at $UserBaseDirectory\Autoload\Autoload instead of $UserBaseDirectory\Kernel. It's not clear what these directories are for exactly... – István Zachar Oct 15 '15 at 16:55