Mathematica: HOWTO Autoload the simplest possible .m or .wl Package file with a few custom functions

Webel IT Australia promotes the amazing Mathematica tool and the powerful Wolfram Language and offers professional Mathematica services for computational computing and data analysis. Our Mathematica tips, issue tracking, and wishlist is offered here most constructively to help improve the tool and language and support the Mathematica user community.
DISCLAIMER: Wolfram Research does not officially endorse analysis by Webel IT Australia.
Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
Keywords

So, you know the bare minimum about Mathematica Packages, and you know you should be using those new-fangled Paclet things, but you just want to get the absolute simplest autoloading of a .m or .wl Package file working.

Find your $UserBaseDirectory (just enter that on an input line to see). On a Mac it will be like:

/Users/username/Library/Mathematica
Under there you will find an Autoload folder. Choose an "application" name. (We're not in fact going to create a fully-fledged Application, just a mini one.) For this example we'll choose TestAuto. Make a folder of that name under Autoload:
$UserBaseDirectory/Autoload/TestAuto
Your code (we are "cheating" a bit here and not creating a full Application or Paclet) goes there. It can be a .m or .wl file. It needs a namespace within the application name, we'll choose `MyLib`, and place it in MyLib.wl

(* ::Package:: *)

BeginPackage["TestAuto`MyLib`"]

autoF::usage = "autoF[a,b] Does something with a and b (and will autoload)";

Begin["`Private`"]
autoF[a_,b_] := a^2 + b^2;
End[]
EndPackage[]
You need to create an additional folder Kernel and place an init.m file under it:
$UserBaseDirectory/Autoload/TestAuto/Kernel/init.m
You must pre-load your Package in that init.m file (this is an Autoload convention):

<< TestAuto`MyLib`
That's it. Restart Mathematica, and it will catch MyLib automatically. You can run your function immediately in a notebook without any additional namespace qualifier:

autoF[1,2]

5

You can also see that the Package is loaded by examining this (it might be quite long):


$Packages

...
You can list the functions in your library using this:

?"TestAuto`MyLib`*"

...
Celebrate:
To learn how to create a fancier, more fledged Application, this David And Alice Park essay "A Mathematica Style" is a good place to start.
Relates to
Related notes
Related notes (backlinks)
Related snippets (extracts)
Visit also
Visit also (backlinks)
External links