$ContextPath

$ContextPath

is a global variable that gives a list of contexts to search, before $Context, in trying to find a symbol that has been entered.

Details

  • Each context is specified by a string of the form "name`".
  • The elements of $ContextPath are tested in order to try to find a context containing a particular symbol.
  • $ContextPath is modified by BeginPackage and EndPackage.
  • $ContextPath is a rough analog for Wolfram Language symbols of the "search path" for files in many operating systems.

Examples

open allclose all

Basic Examples  (2)

The default contexts for looking up symbols:

The symbol Integrate can be found because it is in the "System`" context:

If a symbol with a given name is not found in one of the contexts in $ContextPath, it will be created:

It is created in the context given by $Context:

Turn off the message for new symbol creation:

Scope  (2)

$ContextPath must always be a list of strings:

Both simple and compound contexts can be included in $ContextPath:

Applications  (1)

Add Developer` context to the context path:

PackedArrayForm is a command in the Developer` context that can now be used directly:

Restore the previous context path:

Properties & Relations  (5)

Symbols on $ContextPath are found before symbols in $Context with the same name:

System`Grad is found because "System`" is by default a member of $ContextPath:

Contexts on $ContextPath are searched in order:

Reversing the order of "cont1`" and "cont2`" changes which x is found:

If an input symbol contains context marker (`), $ContextPath is ignored:

The variable x in the current context is found:

However, inner`x is not appended to "outer`", which is on the path, to form outer`inner`x:

BeginPackage changes $ContextPath to consist of "System`" and the package context:

EndPackage restores the previous value of $ContextPath with the package context prepended:

Begin does not alter the value of $ContextPath:

Neither does End:

Introduced in 1988
 (1.0)
 |
Updated in 2007
 (6.0)