PackageImport["context`"]
loads an appropriate file if the specified context is not already in $Packages.
PackageImport["context`""alias`"]
loads the specified context and establishes alias as a context alias for that context.
PackageImport["context`",symbol]
loads the specified context but makes only the given symbol visible in the current package file.
PackageImport["context`",{sym1,sym2,…}]
loads the specified context but makes only the given symbols visible in the current package file.
PackageImport
PackageImport["context`"]
loads an appropriate file if the specified context is not already in $Packages.
PackageImport["context`""alias`"]
loads the specified context and establishes alias as a context alias for that context.
PackageImport["context`",symbol]
loads the specified context but makes only the given symbol visible in the current package file.
PackageImport["context`",{sym1,sym2,…}]
loads the specified context but makes only the given symbols visible in the current package file.
Details and Options
- PackageImport is used in packages written using the Structured Package Format to load external contexts, replacing the use of Needs (although Needs still works).
- When evaluated at an interactive prompt or called in a package that does not use the Structured Package Format, PackageImport["context`"] becomes equivalent to Needs["context`"].
- Specifying a symbol or list of symbols in PackageImport ("import by name") is only possible in packages that use the Structured Package Format.
- You can have multiple calls to PackageImport for the same context in a package file, each specifying a different symbol or list of symbols to import.
- PackageImport only makes the specified context and symbols visible in the current file of the package.
Examples
open all close allBasic Examples (4)
When called with just a context, PackageImport behaves like Needs:
PackageImport["HypothesisTesting`"]MemberQ[$ContextPath, "HypothesisTesting`"]An important feature of PackageImport is that it allows "import by name" of only specific symbols from an external package. This feature of importing only a subset of symbols requires that PackageImport is called from a file in a package that uses the Structured Package Format.
The PackageImportExample directory in ExampleData contains a simple package in the Structured Package Format (SPF) style. This is the contents of the WolframMark.wl file from the package, which defines a WolframMark function:
PackageImport["Benchmarking`", Benchmark]
PackageExported[WolframMark]
WolframMark[] :=
Module[{bench},
bench = First[Benchmark[]];
{"WolframMark" -> "BenchmarkResult" /. bench, "DetailedResults" -> BenchmarkReport[bench]}
]
BenchmarkReport[bench_] := "Results" /. bench
This package uses functions from the standard Benchmarking` package, so it calls PackageImport to load that package and make its symbols visible within the file. The symbol you will want to use from the Benchmarking` package is Benchmark, but the Benchmarking` package also exports a BenchmarkReport function, which happens to conflict with a BenchmarkReport function that is already defined as a local function within the WolframMark.wl file. To solve this problem, the file calls PackageImport["Benchmarking`",Benchmark] to import only the Benchmark symbol.
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["PackageImportExample`"]The function works as expected, showing that the BenchmarkReport function used is the one defined in the file, not the one from the Benchmarking` package:
WolframMark[]It is an error to call PackageImport for a symbol that does not exist in the imported package. Create a temporary package file and use PackageInitialize to load it as a package:
tempdir = CreateDirectory[];
tempfile = FileNameJoin[tempdir, "PackageImportExample.wl"];
WriteString[tempfile,
"PackageImport[\"Benchmarking`\", DoesntExist]"];
Close[tempfile];
PackageInitialize["TempContext`", tempdir];PackageImport works with context aliases in the same way Needs does.
Read in the Computer Arithmetic package with the alias ca`:
PackageImport["ComputerArithmetic`" -> "ca`"]Symbols can be accessed using the context alias:
? ca`UlpThe package's actual context, ComputerArithmetic`, is not added to $ContextPath when an alias is specified:
MemberQ[$ContextPath, "ComputerArithmetic`"]Properties & Relations (1)
If you have external contexts that need to be made visible in all files of your package, you can either put a PackageImport call into every file or use the "HiddenImports" property in your PackageInitialize call:
PackageInitialize["MyPackage`", <|"HiddenImports" -> {"SomeOtherContext`"}|>]
Tech Notes
Related Guides
History
Text
Wolfram Research (2026), PackageImport, Wolfram Language function, https://reference.wolfram.com/language/ref/PackageImport.html.
CMS
Wolfram Language. 2026. "PackageImport." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PackageImport.html.
APA
Wolfram Language. (2026). PackageImport. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PackageImport.html
BibTeX
@misc{reference.wolfram_2026_packageimport, author="Wolfram Research", title="{PackageImport}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/PackageImport.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_packageimport, organization={Wolfram Research}, title={PackageImport}, year={2026}, url={https://reference.wolfram.com/language/ref/PackageImport.html}, note=[Accessed: 12-June-2026]}