This question already has an answer here:

I have problems with name space shadowing which I do not know how to resolve. I have two packages which depend on each other. Here is example of two files


top::usage = "This is top level funciton";
foo::usage = "Text";

foo[arg___]:=Print["This is foo!"];



BeginPackage["barPackage`", {"fooPackage`"}];
bar::usage = "";

bar[arg___] := (Print["This is bar!"]; foo[arg])


Now if I use it as I intended:


Needs["fooPackage`", "/home/johu/temp/fooPackage.m"]

$ContextPath[[;; 2]]
This is bar!
{"fooPackage`", "barPackage`"}

I would get bar working if I loaded another package as top level which however does not contain the definition top


Needs["barPackage`", "/home/johu/temp/barPackage.m"]

$ContextPath[[;; 2]]
This is bar!
This is foo!
{"barPackage`", "fooPackage`"}

My question is how to avoid this shadowing or what would be the cleanest workaround?

Perhaps you can also explain, why in the first case does it even make a name for local symbol Private`foo[]? It is never defined.

Here are some references to relevant educational sources, which however did not solve my confusion


marked as duplicate by Leonid Shifrin, m_goldberg, Michael E2, Community Jun 13 '15 at 18:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Actually, I just realized that this was asked already before. I will port this version of my answer to that post, which contains a less extensive one. $\endgroup$ – Leonid Shifrin Jun 13 '15 at 17:02
  • $\begingroup$ Yes, the discussion was very relevant. I exactly have the mainPackage & helperPackage issue. Solution 2 from the referred is the most acceptable one. I guess I should accept the duplicate tag then? $\endgroup$ – Johu Jun 13 '15 at 17:24
  • $\begingroup$ Good to know it helped. Re: accept - you don't have to do anything. If you want, you can become one of the closers for this question, by adding your vote to close this one as a duplicate (if you have the right to close questions, I don't remember how much rep it takes for that). If you feel like it, you can vote for that discussion question / answer. One thing though: please don't delete this one - it will serve as another gateway to that discussion. $\endgroup$ – Leonid Shifrin Jun 13 '15 at 17:27