6
$\begingroup$

Help says:

NotebookEvaluate can take either a NotebookObject or a filename referencing a notebook file.

therefore, I wonder why

SetDirectory @ NotebookDirectory[];
nbFile = "aNotebook.nb";
FileExistsQ[nbFile]
NotebookEvaluate[nbFile]

returns

Out[3]= True
During evaluation of In[1]:= NotebookEvaluate::nbnfnd: Unable to find the notebook aNotebook.nb. >>
Out[4]= $Failed

and, on the contrary,

SetDirectory @ NotebookDirectory[];
nbFile = FileNameJoin @  {Directory[], "aNotebook.nb"};
FileExistsQ[nbFile]
NotebookEvaluate[nbFile]

returns

True

It looks like NotebookEvaluate could not take relative paths; even worse, the notebook aNotebook.nb contains simply

f[x_]:=x+1;

but, even in the latter case, the function f remains undefined.

$\endgroup$

2 Answers 2

6
$\begingroup$

The problem is that FrontEnd related functions do not respect Kernel related settings.

I do not understand why this is not uniform. It is not clearly stated (I have not found it) which functions are FE related. Of course, it is easy to guess but that is not what you expect, the more that there is an appropriate tutorial which should explain that: NamingAndFindingFiles.

Ok, enough whining, I am talking about functions like:

NotebookSave NotebookOpen NotebookEvaluate NotebookImport

and the directory search stack for them is given by NotebookPath. So this should do:

PrependTo[
   CurrentValue[$FrontEndSession, "NotebookPath"],
   NotebookDirectory[]
]

NotebookOpen[nbFile]
$\endgroup$
4
  • $\begingroup$ Huh. Why would one need the front end to NotebookEvaluate? What's the appropriate way to do what OP is doing (evaluating a notebook with a definition for f) without invoking front end functions? $\endgroup$
    – LLlAMnYP
    Mar 16, 2016 at 7:05
  • $\begingroup$ @LLlAMnYP FrontEnd parses boxes and sends the result to the kernel. You can do this from the Kernel but by UsingFrontEnd. $\endgroup$
    – Kuba
    Mar 16, 2016 at 7:09
  • $\begingroup$ Ah, of course, because notebooks are made of boxes, unlike packages with code cells. I was thinking of Get, but it obviously wouldn't work. $\endgroup$
    – LLlAMnYP
    Mar 16, 2016 at 7:16
  • $\begingroup$ Thanks for replies !! I' m sorry for the repeating, but I' m still incapable to understand how comes that FrontEnd is involved in commands that, evidently misunderstanding, I thought to be kernel related only. Please, can you explain a bit further ? $\endgroup$ Mar 16, 2016 at 7:33
5
$\begingroup$

In case anyone else stumbles across this problem: a possible workaround is given by the function AbsoluteFilename. Given a relative path it can construct the absolute one, which in return causes no problems for NotebookEvaluate.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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