是一个全局变量,它提供在 $Context 之前待搜索的上下文列表以试图找到已经输入的符号.
$ContextPath
是一个全局变量,它提供在 $Context 之前待搜索的上下文列表以试图找到已经输入的符号.
更多信息
- 每个上下文由 "name`" 形式的字符串指定.
- 测试 $ContextPath 的元素是为了找到包含一个特殊符号的上下文.
- $ContextPath 由 BeginPackage 和 EndPackage 修改.
- $ContextPath 与许多操作系统文件中“搜索路径”的 Wolfram 语言符号大致类似.
范例
打开所有单元 关闭所有单元基本范例 (2)
$ContextPath可以找到符号 Integrate,因为它在 "System`" 上下文:
Context[Integrate]如果在 $ContextPath 中的某个上下文中找不到具有给定名称的符号,则会创建它:
On[General::newsym]x新符号在 $Context 给出的上下文下创建:
Context[x] === $ContextOff[General::newsym]范围 (2)
$ContextPath 必须始终是字符串列表:
$ContextPath = "System`"$ContextPath = {"System`"}简单和复合上下文都可以包含在 $ContextPath 中:
$ContextPath = {"simpleContext`", "compound`context`", "System`"}应用 (1)
$ContextPath = Prepend[$ContextPath, "Developer`"];PackedArrayForm 是在 Developer` 上下文中的命令,可直接使用:
PackedArrayForm[Table[Range[i], {i, 5}]]$ContextPath = Rest[$ContextPath];属性和关系 (6)
$ContextPath 中的符号在 $Context 中具有相同名称的符号之前被找到:
$Context = "MyContext`";MyContext`Grad = 5;{Grad, MyContext`Grad}System`Grad 被找到的原因是 "System`" 默认情况下是 $ContextPath 的成员:
Context[Grad]MemberQ[$ContextPath, "System`"]按顺序搜索位于 $ContextPath 的上下文:
cont1`x = 5;
cont2`x = 7;
$ContextPath = {"cont1`", "cont2`", "System`"};
x反转 "cont1`" 和 "cont2`" 的顺序改变了被找到的 x:
$ContextPath = {"cont2`", "cont1`", "System`"};
x如果输入符号含有上下文标记 (`),则忽略 $ContextPath:
$ContextPath = {"outer`", "System`"};
$Context = "outer`inner`";
outer`inner`x = 5x然而,inner`x 不会被追加到路径上的 "outer`" 而形成 outer`inner`x:
inner`xBeginPackage 改变 $ContextPath,以包含 "System`" 和程序包上下文:
$ContextPath = {"System`", "Global`"}BeginPackage["PackageContext`"];
$ContextPathEndPackage 恢复 $ContextPath 的先前值,并在前面加上软件包的上下文:
EndPackage[];
$ContextPathBegin 不会更改 $ContextPath 的值:
$ContextPath = {"System`", "Global`"}Begin["NewContext`"];
$ContextPathEnd 也不会:
End[];
$ContextPath有效的上下文别名无法被添加到 $ContextPath 中:
$ContextAliases["abc`"] = "NewContext`";
AppendTo[$ContextPath, "abc`"];
$ContextPath技术笔记
-
▪
- 上下文 ▪
- 上下文和程序包 ▪
- 建立 Wolfram 语言程序包
相关指南
-
▪
- 命名空间的管理 ▪
- 程序包开发 ▪
- Wolfram 系统会话信息
相关的工作流程
- 对不同的笔记本自动使用独立的上下文环境 (context) ▪
- 处理符号名称遮盖的问题
历史
1988年引入 (1.0) | 在以下年份被更新:2007 (6.0)
文本
Wolfram Research (1988),$ContextPath,Wolfram 语言函数,https://reference.wolfram.com/language/ref/$ContextPath.html (更新于 2007 年).
CMS
Wolfram 语言. 1988. "$ContextPath." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2007. https://reference.wolfram.com/language/ref/$ContextPath.html.
APA
Wolfram 语言. (1988). $ContextPath. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/$ContextPath.html 年
BibTeX
@misc{reference.wolfram_2026_$contextpath, author="Wolfram Research", title="{$ContextPath}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/$ContextPath.html}", note=[Accessed: 16-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_$contextpath, organization={Wolfram Research}, title={$ContextPath}, year={2007}, url={https://reference.wolfram.com/language/ref/$ContextPath.html}, note=[Accessed: 16-August-2026]}