13
$\begingroup$

Documentation flaw introduced before 9.0 and persisting through V11.0.0


In the documentation for Get it says:

If the file found by <<name is a directory, the Wolfram Language will try to load the file init.m in that directory.

So I make a folder called test with a single file called init.m whose only contents are

Print["It works."]

In the same directory as the folder test I open a notebook and run

SetDirectory[NotebookDirectory[]];
<<test

which returns Get::noopen: Cannot open test. (Note that <<test/init.m works as expected).

What gives?

Edit: This has been filed as a documentation bug to Wolfram.

$\endgroup$
  • 1
    $\begingroup$ @Kuba But also Get["test", Path -> {NotebookDirectory[]}] does not work. I think it should? $\endgroup$ – Mr.Wizard Feb 20 '15 at 19:59
  • $\begingroup$ @Mr.Wizard Good point :/ $\endgroup$ – Kuba Feb 20 '15 at 20:07
  • $\begingroup$ @Kuba Sorry, I didn't understand that last comment. $\endgroup$ – Mr.Wizard Feb 20 '15 at 20:17
  • $\begingroup$ related $\endgroup$ – Kuba Feb 21 '15 at 9:31
  • $\begingroup$ related: 64739 $\endgroup$ – Kuba Feb 21 '15 at 9:56
16
$\begingroup$

Shortly:

automatic behavior happens when you use a backtick `.


I think there are multiple typos in docs for Get. I added Test2 dir and and Test2\init.m file to the first path in $Path. In my case Links dir.

According to Get documentation <<Test2 should get it. It won't but <<Test2` will.

Additionally this will work, even without SetDirectory:

Get["Test`", Path -> {NotebookDirectory[]}]

and this will work automatically too:

SetDirectory @ NotebookDirectory[];
<< Test` 

So the difference is in `.

More accurate in description is tutorial/FilesForPackages:

The typical sequence of files looked for by <<name`:

  • name.mx file in DumpSave format

  • name.mx/$SystemID/name.mx file in DumpSave format for your computer system

  • name.m file in Wolfram Language source format

  • name/init.m initialization file for a particular directory

  • dir/… files in other directories specified by $Path

<…>

If name is a directory, then the Wolfram Language will try to load the initialization file init.m in that directory.

So the point is that automatic behaviour happens when you use a backtick `.

$\endgroup$
  • 1
    $\begingroup$ I think this question should be marked as a documentation bug. The behavior you illustrate is the one we are all familiar with from Get and Needs. $\endgroup$ – Mr.Wizard Feb 20 '15 at 21:38
  • 2
    $\begingroup$ Another case in <<nameis: name/Kernel/init.m $\endgroup$ – Murta Feb 21 '15 at 1:12
  • $\begingroup$ @Kuba thanks for getting to the bottom of this...I filed a bug. $\endgroup$ – Ian Hincks Feb 21 '15 at 5:06
  • 1
    $\begingroup$ @Kuba: I think Murta just wanted to note that name/Kernel/init.m is another file which Mathematica tries to open when you use Get["name`"]. I think that this is the older convention for where Mathematica looks for such a file and it will use that before name/init.m if both exist. The answer to "why" is of course pure convention, but I guess it has to do with the fact that WRI wants you to be able to distinguish between FrontEnd/init.m and Kernel/init.m within your package... $\endgroup$ – Albert Retey Feb 21 '15 at 11:17
  • 1
    $\begingroup$ @Kuba, got the reply from Wolfram, they said someone had already filed it. $\endgroup$ – Ian Hincks Mar 3 '15 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.