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?
1 Answer
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
andKernel/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:
-
3$\begingroup$ One can also argue that "Kernel/init.m files in Autoload directories" can be interpreted as
$Installation | $UserBase | $Base / Autoload / Kernel / init.m
rather 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. $\endgroup$– KubaCommented May 13, 2016 at 6:30
init.m
file is required. Normally a package can be loaded with<<Pack`
even without aninit.m
file. But apparently not from Autoload. $\endgroup$