4
$\begingroup$

I have some notebooks belonging to a project. I want them to share definitions with each other, but not with other notebooks.

Here is one approach that I thought would accomplish this: go to Evaluation > Kernel Configuration Options and add a new kernel, say mykernel. Then in each project notebook execute SetOptions[EvaluationNotebook[], Evaluator -> "mykernel"] to reset the kernel for that notebook.

To my surprise, symbol definitions appear to be shared across kernels. That is, if I have a non-project notebook running (Evaluation > Notebook's Kernel is local), it still sees all the defintions in the project notebooks.

I seem to have misunderstood what it means to assign a new kernel, which I though was equivalent to say "run in a separate process". I did not find the documentation helpful in understanding what is going on here. How can I get what I want?

Edit:

I had closed and restarted Mma once to check on this behavior, and I still got the reported behavior. After Kuba's comments I did so again (as part of a more extensive experiment), and now I cannot replicate the reported behavior. I will edit this question again if I see it again (and would appreciate any confirming reports, especially if the cause is discovered).

Edit 2:

Motivation for use of separate kernel: privacy. (I don't want to worry about name clashes.)

Why not use contexts via Begin? As far as I can tell, Begin is almost completely unsafe for this purpose. Example:

ClearAll[f, x]
Begin["MyContext`"]
f[x_] := x^2 + 1
End[]
?? f

We find the definition apparently inside the block is in fact global (because the symbol f already existed at the global level).

$\endgroup$
  • $\begingroup$ I can't reproduce that behavior. What is Options[$FrontEnd, Evaluator]? $\endgroup$ – Kuba Dec 11 '15 at 16:38
  • $\begingroup$ You might want to check stackoverflow.com/questions/4896011/… $\endgroup$ – Marvin Dec 11 '15 at 16:54
  • $\begingroup$ @Kuba Interesting. Although the project notebooks show Evaluation > Notebook's Kernel is mykernel, I see that Options[$FrontEnd, Evaluator] produces {Evaluator -> "Local"}. How to reconcile? (Mma 10.2 on Win 7) $\endgroup$ – Alan Dec 11 '15 at 17:06
  • $\begingroup$ @Kuba So I think you are saying that my expectation was reasonable and what I am experiencing is surprising. $\endgroup$ – Alan Dec 11 '15 at 17:33
  • $\begingroup$ @Alan Indeed :) $\endgroup$ – Kuba Dec 11 '15 at 18:14
2
$\begingroup$

The described method with another Kernel works well for me. But here's another way, set this options for notebook you want to "shield":

SetOptions[
 EvaluationNotebook[],
 {CellProlog :> BeginPackage["c1`"], 
  CellEpilog :> (EndPackage[]; $ContextPath =.)}
 ]

or simply:

SetOptions[EvaluationNotebook[],
  CellContext -> "test`"
]
$\endgroup$
  • $\begingroup$ Maybe just drop c1 from $ContexPath instead of reseting it, your call. $\endgroup$ – Kuba Dec 13 '15 at 14:28
0
$\begingroup$

At the start of each notebook for a particular project, say ProjectNexus add the following Begin["ProjectNexus`"] . Make sure all the notebooks corresponding to the same project are in the same kernel.

$\endgroup$
  • $\begingroup$ As far as I know, all global symbols that reappear inside a Begin block are treated as global rather than context specific. See my edit. $\endgroup$ – Alan Dec 13 '15 at 14:01

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.