However, Needs
is different from Get
in that it expects the context passed to it to appear in $ContextPath
after the package has been loaded. This is why one needs to use BeginPackage
and not merely Begin
when writing a package. BeginPackage
will alsopermanently add that context both to $Packages$ContextPath
and $Packages
. Needs
uses $Packages
to determine if a package has already been loaded and avoid double-loading.
(Something interesting: BeginPackage
also updates Internal`$PackageDependencies
based on its second argument. I do not know where this is used.)
In short, the file namesfile names and the contextcontext of a package doesn'tdon't strictly need to be the same. But if it isn'tthey aren't, Mathematica will sometimes get confused.
All theThe rules described above explain why the standard application directory structure is as described here. There is nothing strictly enforcing this particular structure, but the system is designed in a way that it expects to find this structure in any package.
Some interesting undocumented variables:
Internal`$PackageDependencies
is updated by BeginPackage
based on its second argument
$LoadedFiles
is updated by Get
. To get a more useful list, use Select[$LoadedFiles, Not@StringStartsQ[#, $InstallationDirectory] &]