Skip to content

BeginPackage

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

BeginPackage["ctx`"] sets the current context to "ctx`" and restricts
$ContextPath to {"ctx`", "System`"}, matching Mathematica's package
prologue.
BeginPackage["ctx`", {"need1`", ...}] additionally prepends the
listed contexts to $ContextPath.

Examples

No verified examples yet for this function.

Implementation notes

builtin_begin_package (src/context.c) accepts "ctx"and an optionalNeedslist of context strings, dispatching tocontext_begin_package. That pushes aFRAME_PACKAGEframe (snapshotting context and path), setsg_currentto the absolute package context (relative ``... ` contexts are rejected), clears the search path and rebuilds it as{ctx, System}plus any valid, non-duplicateNeedsentries.republish_statethen republishes$Context/$ContextPath, and the new context is returned.EndPackage[](context_end_package) pops the frame and prepends the closed context to$ContextPath. Invalid specs emitBeginPackage::cxt`.

Attributes: Protected.

Implementation status

Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

References

Notes & additional examples

Worked examples

In[1]:= $Context
Out[1]= "Global`"

In[2]:= BeginPackage["MyPkg`"]
Out[2]= "MyPkg`"

In[3]:= $Context
Out[3]= "MyPkg`"

In[4]:= $ContextPath
Out[4]= {"MyPkg`", "System`"}

Notes

BeginPackage["ctx"]sets$Contexttoctx` and restricts$ContextPathto{"ctx", "System"}, matching Mathematica's package prologue so only system symbols and the package's own symbols resolve under short names. Use the matchingEndPackage[]` to close it.