15

According to the documentation, packages in $UserBaseDirectory/Autoload will load automatically on startup. I placed an .m file in this directory and it did not get loaded. How can I get this to work?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263

1 Answers1

14

In order for a package in Autoload to load, it must have the file Kernel/init.m. Thus,

  • Autoload/MyPack.m will not load.

  • Autoload/MyPack/MyPack.m will also not load.

Instead, we need to have the following structure:

A package file at Autoload/MyPack/Mypack.m and an initialization file for the package at Autoload/MyPack/Kernel/init.m, containing the usual Get["MyPack`MyPack`"].

This is the standard application structure. However, in other situations, simple packages can also be contained in a single .m file, without needing to have a separate directory and init.m for them, so I was surprised that that structure didn't work.

For very small packages which are really just meant to be used as initialization code, we can also use a single file placed at Autoload/MyPack/init.m, without MyPack/MyPack.m or MyPack/Kernel/init.m. This file could be loaded manually by <<MyPack`.

This is documented in the most clear way (to me) here in Wolfram System Sessions, which I found through this post.

On startup, the Wolfram Language kernel does the following:

...

  • Loads init.m and Kernel/init.m files in Autoload directories.

Notes:

  • Autoload packages are loaded even when using the -noinit kernel command line option. This makes them different from $UserBaseDirectory/Kernel/init.m, which isn't loaded with -noinit.

Related:

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 3
    One can also argue that "Kernel/init.m files in Autoload directories" can be interpreted as $Installation | $UserBase | $Base / Autoload / Kernel / init.m rather than Autoload / _ / Kernel / init.m. Maybe it's obvious but only if you know it is... When I was learning things from docs I always wondered if strange wording has a purpose or is a result of lack of time of an author. – Kuba May 13 '16 at 06:30