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?
- 234,956
- 30
- 623
- 1,263
1 Answers
In order for a package in Autoload to load, it must have the file Kernel/init.m. Thus,
Autoload/MyPack.mwill not load.Autoload/MyPack/MyPack.mwill 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.mandKernel/init.mfiles in Autoload directories.
Notes:
- Autoload packages are loaded even when using the
-noinitkernel command line option. This makes them different from$UserBaseDirectory/Kernel/init.m, which isn't loaded with-noinit.
Related:
-
3One can also argue that "Kernel/init.m files in Autoload directories" can be interpreted as
$Installation | $UserBase | $Base / Autoload / Kernel / init.mrather thanAutoload / _ / 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
init.mfile is required. Normally a package can be loaded with<<Pack`even without aninit.mfile. But apparently not from Autoload. – Szabolcs May 12 '16 at 12:42