This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Modularity and the Naming of Things  /

2.6.9 Contexts and Packages

A typical package written in Mathematica introduces several new symbols intended for use outside the package. These symbols may correspond for example to new functions or new objects defined in the package.
There is a general convention that all new symbols introduced in a particular package are put into a context whose name is related to the name of the package. When you read in the package, it adds this context at the beginning of your context search path $ContextPath.

  • This reads in a package for doing Laplace transforms.
  • In[1]:= <<Calculus`LaplaceTransform`

  • The package prepends its context and related ones to $ContextPath.
  • In[2]:= $ContextPath


  • The symbol LaplaceTransform is in the context set up by the package.
  • In[3]:= Context[LaplaceTransform]


  • You can refer to the symbol using its short name.
  • In[4]:= LaplaceTransform[t^3, t, s]


    The full names of symbols defined in packages are often quite long. In most cases, however, you will only need to use their short names. The reason for this is that after you have read in a package, its context is added to $ContextPath, so the context is automatically searched whenever you type in a short name.
    There is a complication, however, when two symbols with the same short name appear in two different packages. In such a case, Mathematica will warn you when you read in the second package. It will tell you which symbols will be "shadowed" by the new symbols that are being introduced.

  • The symbol LaplaceTransform in the context Calculus`LaplaceTransform` is shadowed by the symbol with the same short name in the new package.
  • In[5]:= <<NewLaplace`

    LaplaceTransform::shdw: Symbol LaplaceTransform appears in multiple contexts {NewLaplace`, Calculus`LaplaceTransform`}; definitions in context NewLaplace` may shadow or be shadowed by other definitions.

  • You can access the shadowed symbol by giving its full name.
  • In[6]:= Calculus`LaplaceTransform`LaplaceTransform[t^3, t, s]


    Conflicts can occur not only between symbols in different packages, but also between symbols in packages and symbols that you introduce directly in your Mathematica session. If you define a symbol in your current context, then this symbol will shadow any other symbol with the same short name in packages that you read in. The reason for this is that Mathematica always searches for symbols in the current context before looking in contexts on the context search path.

  • This defines a function in the current context.
  • In[7]:= Div[f_] = 1/f


  • Any other functions with short name Div will be shadowed by the one in your current context.
  • In[8]:= <<Calculus`VectorAnalysis`

    Div::shdw: Symbol Div appears in multiple contexts {Calculus`VectorAnalysis`, Global`}; definitions in context Calculus`VectorAnalysis` may shadow or be shadowed by other definitions.

  • This sets up the coordinate system for vector analysis.
  • In[9]:= SetCoordinates[Cartesian[x, y, z]]


  • This removes Div completely from the current context.
  • In[10]:= Clear[Div]; Remove[Div]

  • Now the Div from the package is used.
  • In[11]:= Div[{x, y^2, x}]


    If you get into the situation where unwanted symbols are shadowing the symbols you want, the best thing to do is usually to get rid of the unwanted symbols using Remove[s]. An alternative that is sometimes appropriate is to rearrange the entries in $ContextPath and to reset the value of $Context so as to make the contexts that contain the symbols you want be the ones that are searched first.

    Getting a list of packages.