PackageScoped[symbol]
makes symbol visible in all files of the current package, but not visible to users of the package.
PackageScoped[{sym1,sym2,…}]
makes multiple symbols visible in all files of the current package.
PackageScoped
PackageScoped[symbol]
makes symbol visible in all files of the current package, but not visible to users of the package.
PackageScoped[{sym1,sym2,…}]
makes multiple symbols visible in all files of the current package.
Details
- PackageScoped works only in files that use the Structured Package Format.
- Symbols named in PackageScoped statements are created in a special shared context visible in all files of your package, but not visible outside of the package.
- The symbol argument can be a symbol or a string.
- PackageScoped statements allow you to share function definitions among all the files in a package, but not expose them outside the package.
- PackageScoped statements can be placed wherever you like in your package: close to where the definitions for the symbol appear, collected at the top of each file, collected into one file of your package, etc.
- PackageScoped statements are read in a special way and must appear in files on a line of their own.
- Any symbol not named in a PackageScoped or PackageExported statement is private to the file in which it appears.
Examples
Basic Examples (3)
The SPFExample directory in ExampleData contains an extremely simple package in the Structured Package Format (SPF) style. This is the one-line contents of the init.wl file:
PackageInitialize["SPFExample`"]
When the init.wl file loads, PackageInitialize gathers up the surrounding source files and loads them to become the SPFExample` package.
This is the contents of the PackageFile1.wl file:
PackageExported[Func1]
Func1[x_] := x+1
PackageScoped[utilFunc]
utilFunc[n_] := n^2
This is the contents of the PackageFile2.wl file:
PackageExported[Func2]
Func2[x_] := utilFunc[x]
Because the symbol utilFunc is named in a PackageScoped statement, it is visible in all files of the package, which is why it can be used within the definition of Func2, despite being defined in a different file.
To use this example, first add its parent directory to $Path so that it can be found via a context name:
AppendTo[$Path, FileNameJoin[$InstallationDirectory, "Documentation", "English", "System", "ExampleData"]];Now load the package in the usual way:
Needs["SPFExample`"]The Func2 function works properly, showing that the definition of utilFunc was visible in PackageFile2.wl:
Func2[3]PackageScoped is only meaningful when called from a file of code that is part of a package, not during interactive input:
PackageScoped[SomeSymbol]PackageScoped statements must appear alone on their own line. Create a temporary file containing an improper PackageScoped statement and call PackageInitialize to create a package out of it:
tempdir = CreateDirectory[];
tempfile = FileNameJoin[tempdir, "BadPackageScope.wl"];
WriteString[tempfile, "PackageScoped[Foo] + 1"];
Close[tempfile];
PackageInitialize["TempContext`", tempdir];This example is similar to the previous one, demonstrating that you cannot "program" with PackageScoped—it must be written verbatim in the file:
tempdir = CreateDirectory[];
tempfile = FileNameJoin[tempdir, "BadPackageScope.wl"];
WriteString[tempfile, "PackageScoped /@ {Foo, Bar}"];
Close[tempfile];
PackageInitialize["TempContext`", tempdir];Tech Notes
Related Guides
History
Text
Wolfram Research (2026), PackageScoped, Wolfram Language function, https://reference.wolfram.com/language/ref/PackageScoped.html.
CMS
Wolfram Language. 2026. "PackageScoped." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PackageScoped.html.
APA
Wolfram Language. (2026). PackageScoped. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PackageScoped.html
BibTeX
@misc{reference.wolfram_2026_packagescoped, author="Wolfram Research", title="{PackageScoped}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/PackageScoped.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_packagescoped, organization={Wolfram Research}, title={PackageScoped}, year={2026}, url={https://reference.wolfram.com/language/ref/PackageScoped.html}, note=[Accessed: 12-June-2026]}