9
$\begingroup$

I'm loading a package at startup in "~/Library/Mathematica/Kernel.init", which looks like this:

(** User Mathematica initialization file **)
Print["Loading Custom Packages"];
Get["~/my_package.wl"];

Is this not the right place to put this? Because unfortunately, this causes the kernel to hang on startup:

enter image description here

Loading the package after startup works:

(** User Mathematica initialization file **)
load[]:=Get["~/my_package.wl"];

enter image description here

Here are the first two lines of the package:

CurrentValue[$FrontEndSession, InitializationCellWarning] = False;
BeginPackage["CoreUtils`", {"GeneralUtilities`", "DatabaseLink`"}];
$\endgroup$
3
  • $\begingroup$ Can you show what is in that package? $\endgroup$ Apr 13, 2017 at 16:56
  • $\begingroup$ @DanielLichtblau Unfortunately not, it's not mine, it's for work. But I can show the first two lines, see above. $\endgroup$
    – M.R.
    Apr 13, 2017 at 17:20
  • 1
    $\begingroup$ What is of interest is whether it makes use of various functions that require signalling, such as Throw, Break, and the like. These do not work during loading. $\endgroup$ Apr 13, 2017 at 18:43

1 Answer 1

6
$\begingroup$

Not all packages can be loaded on startup. There are some operations that simply do not work during startup, the most prominent one being Throw/Catch.

For this reason, it is generally not a good idea to load packages on startup.

See my questions about this here:

These came up while I was trying to make my own packages "init-resistant".

What can you do to avoid the problem?

What can you do then?

You can use @WReach's suggestion from the post I linked. Load the package in a scheduled task:

RunScheduledTask[
  (* perform some complex initialization *)
; RemoveScheduledTask @ $ScheduledTask
, {0}
]

This did not work for me unless I set a non-zero value for the delay. It did work when I set it to 1 second.

But I am not comfortable with this solution. What if the kernel startup is triggered by evaluating a cell which has code that relies on the package you are loading?

Aside: I have also seen it many times that starting up the kernel by evaluating certain cells will trigger crashes or other misbehaviour sometimes. This was affecting some of my notebooks when I was using version 8 on a not very fast computer. Since then I did not encounter this frequently, though 11.1 tends to randomly start up very slowly for me when kernel start is triggered by cell initialization. I have no proof, but all these problems could be explained by such scheduled tasks ... if such things are used internally within Mathematica. I am just guessing that this might be related to scheduled tasks, and I am probably wrong. Do not take it too seriously.

Another thing you can do is use DeclarePackage and list every symbol in that package. Then the package will load only when you actually use something from it.

What can the package author do to avoid the problem?

The best solution would be to ask the person who wrote the package to make it init-friendly. I wrote about how to do this here:

For most packages this is easy. For others, not so much.

$\endgroup$

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.