I'm trying to find a solution to a problem that I've been struggling with for a while. I asked a variant of the question here: Automatically load init.m without sharing CellContext, but think I've clarified what I'm trying to do enough that it warrants a new question - if this is not the case that's fine.
Basically, I have three requirements I'm trying to accomplish:
- I want all new notebooks created to automatically only have their own context - I don't like sharing variables/functions between different notebooks.
- I want all notebooks to automatically load a package created by me which contains a set of variables/functions that I do want common to all notebooks.
- I do not want local variables from my package to load into newly created notebooks.
I can accomplish (1) easily by setting CellContext to Notebook from the options inspector. I can accomplish (2) by putting Needs["MyPackage"] in my init.m. However, so far when I've tried I can't get both to work. If the CellContext is globally set to Notebook, when I load up a new notebook and type in a variable (say, XX) from MyPackage, the autocomplete will recognize XX in the context of MyPackage, but evaluating this variable returns nothing - i.e the variable is still undefined despite the autocomplete recognizing it.
The other annoyance is that in the case where I do have CellContext set to global, not only are all the functions/variables from MyPackage available in newly created notebooks, but the local variables are as well! In other words, let's say I have a function f[xx_] in MyPackage. Not only will the the variable autocomplete recognize f in the context of MyPackage, but it will recognize xx as well. Executing xx will return no value but the syntax will be highlighted black like it is a defined variable.
I feel like I'm just fundamentally misunderstanding something about how contexts/packages work in Mathematica, and have been unable to accomplish what seems to be a fairly simple set of goals. Any advice on how to set up my configuration would be greatly appreciated. I've read through most/all of the documentation on Packages/Contexts but obviously have missed something.
BeginPackage["MyPackage`"],Begin["`Private`"],End[]andEndPackage[]in your package file, when you load<<MyPackage`it'll work cleanly without showing all those random other symbols – b3m2a1 Feb 18 '20 at 22:25