({{, (RefLink[$ContextPath, paclet:ref/$ContextPath])
is a global variable that gives a list of contexts to search, before (RefLink[$Context, paclet:ref/$Context]), in trying to find a symbol that has been entered. }})

OpenersNotesSection(Spacer1)Details

Each context is specified by a string of the form "name`".

The elements of (RefLink[$ContextPath, paclet:ref/$ContextPath]) are tested in order to try to find a context containing a particular symbol.

(RefLink[$ContextPath, paclet:ref/$ContextPath]) is modified by (RefLink[BeginPackage, paclet:ref/BeginPackage]) and (RefLink[EndPackage, paclet:ref/EndPackage]).

(RefLink[$ContextPath, paclet:ref/$ContextPath]) is a rough analog for Wolfram Language symbols of the "search path" for files in many operating systems.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

The default contexts for looking up symbols:

$ContextPath

The symbol (RefLink[Integrate, paclet:ref/Integrate]) can be found because it is in the "System`" context:

Context[Integrate]

If a symbol with a given name is not found in one of the contexts in (RefLink[$ContextPath, paclet:ref/$ContextPath]), it will be created:

On[General::newsym]

x

It is created in the context given by (RefLink[$Context, paclet:ref/$Context]):

Context[x]===$Context

Turn off the message for new symbol creation:

Off[General::newsym]

OpenersExampleSection1(Spacer1)Scope  (2)

(RefLink[$ContextPath, paclet:ref/$ContextPath]) must always be a list of strings:

$ContextPath="System`"

$ContextPath={"System`"}

Both simple and compound contexts can be included in (RefLink[$ContextPath, paclet:ref/$ContextPath]):

$ContextPath= {"simpleContext`", "compound`context`", "System`"}

OpenersExampleSection2(Spacer1)Applications  (1)

Add Developer` context to the context path:

$ContextPath = Prepend[$ContextPath,"Developer`"];

(PackageLink[PackedArrayForm, paclet:Developer/ref/PackedArrayForm, Developer Package Symbol]) is a command in the Developer` context that can now be used directly:

PackedArrayForm[Table[Range[i],{i,5}]]

Restore the previous context path:

$ContextPath = Rest[$ContextPath];

OpenersExampleSection3(Spacer1)Properties & Relations  (5)

Symbols on (RefLink[$ContextPath, paclet:ref/$ContextPath]) are found before symbols in (RefLink[$Context, paclet:ref/$Context]) with the same name:

$Context="MyContext`";

MyContext`Grad=5;

{Grad,MyContext`Grad}

(RefLink[System`Grad, paclet:ref/Grad]) is found because "System`" is by default a member of (RefLink[$ContextPath, paclet:ref/$ContextPath]):

Context[Grad]

MemberQ[$ContextPath,"System`"]

Contexts on (RefLink[$ContextPath, paclet:ref/$ContextPath]) are searched in order:

cont1`x=5;
cont2`x=7;
$ContextPath = {"cont1`","cont2`","System`"};
x

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

$ContextPath = {"cont2`","cont1`","System`"};
x

If an input symbol contains context marker (`), (RefLink[$ContextPath, paclet:ref/$ContextPath]) is ignored:

$ContextPath={"outer`","System`"};
$Context="outer`inner`";
outer`inner`x=5

The variable x in the current context is found:

x

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

inner`x

(RefLink[BeginPackage, paclet:ref/BeginPackage]) changes (RefLink[$ContextPath, paclet:ref/$ContextPath]) to consist of "System`" and the package context:

$ContextPath={"System`","Global`"}

BeginPackage["PackageContext`"];
$ContextPath

(RefLink[EndPackage, paclet:ref/EndPackage]) restores the previous value of (RefLink[$ContextPath, paclet:ref/$ContextPath]) with the package context prepended:

EndPackage[];
$ContextPath

(RefLink[Begin, paclet:ref/Begin]) does not alter the value of (RefLink[$ContextPath, paclet:ref/$ContextPath]):

$ContextPath={"System`","Global`"}

Begin["NewContext`"];
$ContextPath

Neither does (RefLink[End, paclet:ref/End]):

End[];
$ContextPath

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[$Context, paclet:ref/$Context])  ▪  (RefLink[Contexts, paclet:ref/Contexts])  ▪  (RefLink[Begin, paclet:ref/Begin])  ▪  (RefLink[BeginPackage, paclet:ref/BeginPackage])  ▪  (RefLink[$Packages, paclet:ref/$Packages])  ▪  (RefLink[CellContext, paclet:ref/CellContext])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Contexts, paclet:tutorial/Contexts])}, {▪(RefLinkPlain[Contexts and Packages, paclet:tutorial/ContextsAndPackages])}, {▪(RefLinkPlain[Setting Up Wolfram Language Packages, paclet:tutorial/SettingUpWolframLanguagePackages])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Namespace Management, paclet:guide/NamespaceManagement])}, {▪(RefLinkPlain[Package Development, paclet:guide/PackageDevelopment])}, {▪(RefLinkPlain[Wolfram System Session Information, paclet:guide/WolframSystemSessionInformation])}})}})