Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following simple code which dumps the global context:

var = 5;
DumpSave["", "Global`"];

Now, in a new notebook, I would like to import var in another context. Trying

<< ""

does not work because var remains in the global context. Is there a solution?

Thank you.

share|improve this question

Using Begin and End won't help, because .mx files are lower-level and the way they are loaded is different from normal packages.

I was about to say that this isn't possible, but here is a hack which seems to work:


You can use this as, for example:

loadInContext["MyContext`", ""}]

It is based on a rather interesting and little-known feature, that the assignment like

Context[sym] = newContext

will move the symbol (together with the global rules possibly attached to it) to a new context.

share|improve this answer
Does this prevent a name collision for Symbols already defined in Global? – Mr.Wizard May 1 '14 at 11:39
@Mr.Wizard No, it doesn't. If the symbol has been already defined in Global` , this method won't work for it, because then $NewSymbol won't fire. – Leonid Shifrin May 1 '14 at 11:41
I was afraid of that. +1 for Context[sym] = newContext alone however, which I either wasn't aware of or forgot. – Mr.Wizard May 1 '14 at 11:41
@Mr.Wizard Thanks for the upvote. Very few people know about this Conext[sym]= thing, I think (I mean, outside of WRI). I discovered it myself some time ago, but I am sure I haven't seen any single mention of this thing anywhere on SO, SE of MathGroup. This was a good opportunity to mention it. – Leonid Shifrin May 1 '14 at 11:43
@Mr.Wizard as to failing to remember names, when I first met my wife, I could not remember her name to save my life, and she the same. This was despite the fact we were both interested in each other. For some reason, each others' names took a long time to stick. – rcollyer May 1 '14 at 13:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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