How to reliably reload package after change? [duplicate]

I'm using IntelliJ Idea with a Mathematica plugin to develop Mathematica application. I keep IntelliJ and Mathematica opened at the same time. I would like to change something in the IDE, evaluate notebook in the Mathematica and see updated result, but Mathematica fails to see changes in my code.

My application consists of several subpackages, and Kernel/init.m looks like

Get["MyPackageA"]
Get["MyPackageB"]
...


Notebook is something like

Quit[]
Get["MyPackage"]
SomeFunctionFromMyPackage[]


And I've also tried

Needs["UtilitiesCleanSlate"] (* inside initialization cell *)
CleanSlate[]
Get["MyPackage"]
SomeFunctionFromMyPackage[]


Let's say I changed SomeFunctionFromMyPackage, saved .m file hit "Evaluate notebook" and hoping to see result changing, but this doesn't happen. Result is not getting updated. I've tried to introduce syntax error in function, Mathematica doesn't react at all.

The only way to force Mathematica to actually reload my package is to restart it.

It's driving me crazy, what am I doing wrong?

Upd

Such strange problems were caused by the fact that subpackages inside application depend on each other with Needs instead of Get. When I changed it to Get the problems went away.

marked as duplicate by Leonid Shifrin, Feyre, mikado, Mr.Wizard♦Oct 16 '16 at 0:04

• So you say you actually quit the kernel and loaded the package again, and it didn't see the changes? But when you restarted the front end then it did? That sounds rather unlikely, so please check carefully again. – Szabolcs Oct 15 '16 at 17:44
• @Szabolcs, ok, I checked again. The problem is in "Evaluate notebook". When I manually evaluate cells from top to the bottom, it works as expected. Why? – lambdas Oct 15 '16 at 17:50
• Probably because Quit[] cancels all further evaluations by default. – Szabolcs Oct 15 '16 at 17:51
• @Szabolcs, ouch, I see.. Then my question stays the same, what is convenient and reliable way to reevaluate notebook after package changed? – lambdas Oct 15 '16 at 17:53
• I once wrote a package to cleanly reload packages. A brief description is in this post. – Leonid Shifrin Oct 15 '16 at 18:39

There isn't a fully general way to reload packages because packages can do just about anything in principle, not just create definitions.

I recommend that you write your package in a way that simply using Get again will properly re-set its state. Assuming that all your package does is issue definitions, you could do something like this:

BeginPackage["MyPack"];

Unprotect["MyPack*"];
ClearAll["MyPack*"];
ClearAll["MyPackPrivate*"];

Foo::usage = "";

Begin["Private"];

helper[x_] := x^2
Foo[x_] := 2*helper[x]

End[];
Protect["MyPack*"];
EndPackage[];


But in general there's a lot more a package can do than issue definitions. For example, it can cache results (MaTeX), it can load or save some persistent configuration (MaTeX, MATLink), it can create temporary files and directories to work with, or some other kind of state that persists throughout the session, such as connecting to other programs (MATLink), it may load LibraryLink functions that must be unloaded before they can be safely reloaded (IGraph/M), etc. My point is that most packages that I contributed to had something else than definitions in them that made reloading problematic. Thus not all of them will fully reset their state when using Get a second time, and sometimes I just need to manually quit the kernel before loading them again. But I do try to build in some robustness against reloading to make development easier (and accidental double loading by users should definitely not break things).

• It's a nice solution, thank you! There are no side effects in my packages, so I'll go with it. It turned out that problem is caused by subpackages depending on each other with Needs instead of Get. – lambdas Oct 15 '16 at 20:27
• This does not seem to reload the usage definitions. Is there a workaround? – Michael McCain Jan 3 '17 at 11:34
• @MichaelMcCain Can you show an example? I don't see why it wouldn't enable changing usage messages. – Szabolcs Jan 3 '17 at 11:36
• @Szabolcs - We can use the exact code in the this thread to illustrate. I first executed the package notebook. Then I usedGet["MyPack"] in a new notebook to install it. Then I verified it was installed. Then I tried changing the line in the package where is says Foo::usage = ""; to Foo:usage = "testing". I then re-executed Get["MyPack"] in the other notebook. No change. :/ – Michael McCain Jan 3 '17 at 11:54
• sorry for the quotation single and double quotations problems in my question... not sure how to fix that. – Michael McCain Jan 3 '17 at 11:55

I use these two regularly when developing packages. Not entirely bullet-proof, but enormously helps my workflow. For convenience, you should simply use Reload instead of Needs. Whatever is changed in the package file will be reloaded.

Attributes[Unload] = {Listable};
Unload::nonun = "The System. and Global. contexts cannot be removed.";
Unload["System" | "Global"] := Message[Unload::nonun];
Module[{found = Cases[$Packages, _?(StringMatchQ[#, form<>"*"] &)], all}, all = # <> "*" & /@ found; Unprotect /@ all; Quiet[fun /@ all]; Unprotect@$Packages;
$Packages = DeleteCases[$Packages, Alternatives @@ found];
Protect@$Packages;$ContextPath = DeleteCases[$ContextPath, Alternatives @@ found]; ]; Attributes[Reload] = {Listable}; Reload[pkg_String] := ( If[MemberQ[Union[$ContextPath, \$Packages], _?(StringMatchQ[#, pkg<>"*"] &)],

Note, that Unload removes symbols from other packages as well, e.g. Unload["Language"] also removes other system-Language related functions, not just user-Language ones.
• Thank you for answer, but it doesn't work for me. I defined Reload and Unload in the same notebook, I guess it's ok. And I'm calling Reload["MyPackage"] instead of Get["MyPackage"]. It doesn't give any errors, but symptoms are the same. Does Reload work correctly for subpackages? – lambdas Oct 15 '16 at 19:53
• Sorry, your functions actually work great. My packages depend on each other with Needs instead of Get and it caused strange problems. – lambdas Oct 15 '16 at 20:23