0
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ The output of 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 in Get $\endgroup$ Commented Jan 7, 2021 at 10:47
  • $\begingroup$ @DanielHuber I already know all that. If you examine my test code you will see i did Get["testpack", Path -> $packages]` and it worked but only after I invoked it after I did setPaths[]. 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$ Commented Jan 7, 2021 at 20:19
  • 1
    $\begingroup$ I meant something like: Get["c:/abc/def/testpack.m"] $\endgroup$ Commented Jan 7, 2021 at 20:31
  • $\begingroup$ @DanielHuber i figured out why it wouldn't on another post a user suggested making init a package. $\endgroup$ Commented Jan 27, 2021 at 21:23
  • 1
    $\begingroup$ Good to hear this. $\endgroup$ Commented Jan 27, 2021 at 21:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.