I would like to find a clean way to suppress the loading of a package that's defined in initialization cells of notebooks when the package is already loaded at the time the notebook is initialized. The "obvious" thing to do is to put code something like this in the initialization:
If[!MemberQ[$Packages,"mystuff`"], BeginPackage["mystuff`"]; x::usage="This should go in the context mystuff`."; Begin["`Private`"]; x = a; y = b; End; EndPackage; ]
Unfortunately, the symbols x and y both land in the Global` context instead of mystuff` and mystuff`Private` where they belong. Evidently, the code inside of If uses its own local version of $Context, rather than the global one that's manipulated by BeginPackage and Begin.
I know that I could use Needs if I put the package definitions ins a separate file, but that won't do, since I want the notebooks to work on computers that may not have the package files installed. Any suggestions?