Contexts and Packages

A typical package written in the Wolfram Language 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 (RefLink[$ContextPath, paclet:ref/$ContextPath]).

This reads in a package for proving primality:

<<PrimalityProving`

The package prepends its context to (RefLink[$ContextPath, paclet:ref/$ContextPath]):

$ContextPath

The symbol ProvablePrimeQ is in the context set up by the package:

Context[ProvablePrimeQ]

You can refer to the symbol using its short name:

ProvablePrimeQ[2143]

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 (RefLink[$ContextPath, paclet:ref/$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, the Wolfram Language 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 ProvablePrimeQ in the context PrimalityProving` is shadowed by the symbol with the same short name in the new package:

<<NewPrimalityProving`

You can access the shadowed symbol by giving its full name:

PrimalityProving`ProvablePrimeQ[2143]

Conflicts can occur not only between symbols in different packages, but also between symbols in packages and symbols that you introduce directly in your Wolfram Language session. If you define a symbol in your current context, then this symbol may become shadowed by another symbol with the same short name in packages that you read in. The reason for this is that the Wolfram Language searches for symbols in contexts on the context search path before looking in the current context.

This defines a function in the current context:

ScalarQ[f_]=Head[f]=!=List

This function considers complex numbers scalars:

ScalarQ[2I]

The ScalarQ function in your current context will be shadowed by the one in the package:

<<Quaternions`

The (PackageLink[ScalarQ, paclet:Quaternions/ref/ScalarQ, Quaternions Package Symbol]) from the package is used:

ScalarQ[2I]

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 (RefLink[Remove, paclet:ref/Remove])[s]. An alternative that is sometimes appropriate is to rearrange the entries in (RefLink[$ContextPath, paclet:ref/$ContextPath]) and to reset the value of (RefLink[$Context, paclet:ref/$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.