BeginPackage["context`"]
makes context` and System` the only active contexts.
BeginPackage["context`",{"need1`","need2`",…}]
calls Needs on the needi.
BeginPackage
BeginPackage["context`"]
makes context` and System` the only active contexts.
BeginPackage["context`",{"need1`","need2`",…}]
calls Needs on the needi.
Details
- BeginPackage is typically used at the beginning of a Wolfram Language package.
- BeginPackage resets the values of both $Context and $ContextPath.
- The interpretation of symbol names depends on context. BeginPackage thus affects the parsing of input expressions.
Examples
open all close allBasic Examples (1)
Commands to define a simple package:
BeginPackage["square`"];The context for symbol creation has been changed to square`:
Context[]The context path for symbol lookup includes square`:
$ContextPathHere are the commands defining the function in the package:
square::usage = "square[x] gives x^2";square[x_] := x ^ 2;Restore the context, but leave square` on the context path for symbol lookup:
EndPackage[]Since EndPackage leaves square`square on the context path, the function defined is visible:
? squaresquare[12]Scope (1)
Use BeginPackage and EndPackage to start and end a package in a file:
FilePrint["ExampleData/Collatz.m"]The package can be loaded with Get:
<<ExampleData/Collatz.mOnce loaded, the definitions work:
Collatz[47]Properties & Relations (3)
BeginPackage["cont`"] sets $Context to "cont`":
$Context = "Global`";
BeginPackage["Example`"];
$ContextEndPackage restores $Context to its value before the matching BeginPackage:
EndPackage[]
$ContextBeginPackage["cont`"] sets $ContextPath to {"cont`","System`"}:
$ContextPath = {"Global`", "System`"};
BeginPackage["Example`"];
$ContextPathEndPackage restores $ContextPath to its value before BeginPackage with "cont`" prepended:
EndPackage[]
$ContextPathBeginPackage does not alter the value of $ContextAliases:
$ContextAliases = <|"c`" -> "Context`"|>;
BeginPackage["MyPackage`"];
$ContextAliasesEndPackage restores $ContextAliases to its value before the matching BeginPackage:
Needs["Developer`" -> "d`"];
$ContextAliases["e`"] = "Experimental`";
EndPackage[];
$ContextAliasesSee Also
Tech Notes
Related Guides
Related Workflows
- Create a Package File ▪
- Load a Package ▪
- Handle Shadowing of Symbol Names
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), BeginPackage, Wolfram Language function, https://reference.wolfram.com/language/ref/BeginPackage.html.
CMS
Wolfram Language. 1988. "BeginPackage." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/BeginPackage.html.
APA
Wolfram Language. (1988). BeginPackage. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BeginPackage.html
BibTeX
@misc{reference.wolfram_2026_beginpackage, author="Wolfram Research", title="{BeginPackage}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/BeginPackage.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_beginpackage, organization={Wolfram Research}, title={BeginPackage}, year={1988}, url={https://reference.wolfram.com/language/ref/BeginPackage.html}, note=[Accessed: 13-June-2026]}