The contents of init.m are placed inside an Initialization Cell so that some symbols are created in the Global` context and to prepend alternate paths to $Path for easier management of files during development. I know the code works because I tested it from within a regular notebook (extension nb) then I compared the output of setPaths[] with the raw $Path and all path elements matched. I also loaded an arbitrary test package after evaluating setPaths[] which would only be possible after $addpaths (a short list of directory paths) were prepended to $Path. Problem is that Mathematica completely ignores it.
The contents of Initialization Cell inside init.m:
$addpaths = { $home = "D:\\Google Drive\\005 Projects\\mathematica\\", $packages = FileNameJoin[{$home, "resources", "packages"}] }; Options[setPaths] = {show -> True}; setPaths[p_List : $addpaths, OptionsPattern[]] := Block[{$path, pos}, If[TrueQ[OptionValue[show]], fn = Echo, fn = List]; Map[If[! MemberQ[$Path, #], PrependTo[$Path, #], Null] &, p]; $path = $Path; pos = Sort[Flatten[Map[Position[$Path, #] &, p], 1]]; $path = MapAt[Style[#, RGBColor[.5, .75, 1], Bold] &, $path, pos]; fn[pos, "positions of $addpaths -> "]; fn[Iconize[Column[$path], "$Path"], "new $Path -> "]; ]; setPaths[]; I saved init.m to two locations:
FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}] (* same as *) "C:\\Users\\User\\AppData\\Roaming\\Mathematica\\Kernel\\init.m" FileNameJoin[{$UserBaseDirectory, "FrontEnd", "init.m"}] (* same as *) "C:\\Users\\User\\AppData\\Roaming\\Mathematica\\FrontEnd\\init.m" The test code for evaluating an arbitrary package testpack.m:
FindFile["testpack.m"] Get["testpack`", Path -> $packages] Echo[$ContextPath, "$ContextPath ->"]; testpack[]; Here is the results of evaluating test code as Mathematica starts but before evaluating setPaths[]:
$Failed Get::path: $packages in $Path is not a string. Get::path: $packages in $Path is not a string. Get::noopen: Cannot open testpack`. $Failed $ContextPath -> {DocumentationSearch`,ResourceLocator`,System`,Global`} testpack[] Here is the results of evaluating test code after evaluating setPaths[]:
"D:\\Google Drive\\005 \ Projects\\mathematica\\resources\\packages\\testpack.m" "testpack[] loaded successfully" $ContextPath -> {testpack`,DocumentationSearch`,ResourceLocator`,System`,Global`} "testpack[] loaded successfully" Question: Why isn't my init.m not loading?
FindFile["testpack.m"]is$Failed, indicating that the file has not been found. Type$Path. This gives you the locations that MMA searches for a file. A simple solutions would be to specify the whole path inGet$\endgroup$Get["testpack", Path -> $packages]` and it worked but only after I invoked it after I didsetPaths[]. But it still doesn't answer my question as to why init.m isn't loading. BTW I also tried $UserBaseDirectory\Autoload and that didn't work either. $\endgroup$Get["c:/abc/def/testpack.m"]$\endgroup$