18
$\begingroup$

Is there any way to have the functions from a mathematica file be loaded on startup?

For example, in this excellent answer, Jens helps me out and gives me some functions for creating legends for plots. Instead of loading a file with those functions in it every time I load mathematica, can I instead put all the functions in a file and have them load every time I load mathematica?

$\endgroup$
21
$\begingroup$

There are several configuration files that you can use to load functionality at startup. They have the form

($BaseDirectory | $UserBaseDirectory)/(Kernel | FrontEnd)/init.m

where

$BaseDirectory 

is for every user on the system and

$UserBaseDirectory

is for you along and Kernel or FrontEnd specifies what you are configuring. In fact, a lot of the settings under the Preferences menu automatically write to $UserBaseDirectory/FrontEnd/init.m.

In your case, you are looking to add to $UserBaseDirctory/Kernel/init.m.

$\endgroup$
  • 7
    $\begingroup$ FileNameJoin[{$UserBaseDirectory,"FrontEnd","init.m"}] for OS independency. $\endgroup$ – Sjoerd C. de Vries Jun 6 '12 at 20:47
17
$\begingroup$

With $UserBaseDirectory/Kernel/init.m the most obvious place to put code which should be autoloaded has been mentioned.

But I think it might be worth mentioning that there is also the Autoload directory within $UserBaseDirectory where you can put any package file or package directory and those will automatically be loaded at startup. For the purpose mentioned I think that might be an even better place (after having the code set up as a proper package of course...).


Note that init.m files and Autoload packages are loaded during kernel initialization when certain functions, such as Throw/Catch, cannot be safely evaluated. A lot of complex functionality depends on these, and is therefore unsafe to use in init files. It is generally best to limit initialization files to issuing definitions and basic operations.


The initialization is done in this order:

  • load paclet manager
  • load $BaseDirectory/Kernel/init.m
  • load $UserBaseDirectory/Kernel/init.m
  • load files passed with the -initfile command line option
  • load Autoload packages

The loading is triggered in SystemFiles/Kernel/SystemResources/$SystemID/sysinit.m, which you can examine for more information on the initialization process. Some documentation is here.

Subkernels used by the parallel tools are launched with -noinit, so they don't load the standard Kernel/init.m files. They do load Autoload packages.

$\endgroup$
  • $\begingroup$ Hope you don't mind the edit, feel free to revert. $\endgroup$ – Szabolcs Nov 30 '16 at 15:12
  • $\begingroup$ no, it just added useful information that I was too lazy too lookup or check. As you did contribute more than me to the answer, I feel pressed to turn this into a public wiki. If you find something else to add, I'll do so :-) $\endgroup$ – Albert Retey Nov 30 '16 at 15:27
  • $\begingroup$ It's up to you if you want to change it to wiki. I absolutely don't mind if you don't change it. $\endgroup$ – Szabolcs Nov 30 '16 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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