我如何以编程方式检查我是 julia 笔记本中的 运行 代码?

How can I programmatically check that I am running code in a notebook in julia?

我需要以编程方式检查我是否在 Julia 的 jupyter notebook 中 运行ning 代码。一种方法是使用

isdefined(Main, :IJulia)

然而,这不适用于 vscode 内的笔记本,因为它们是 运行 来自 IJulia 之外的,是否有适用于这种情况的检查?

@__FILE__ 这会在 Julia REPL 中产生 REPL[_],在 Jupyter 中产生 In[_],在 Pluto 中产生“/path/to/file.jl#==#hashocde” 所以测试可能是:

match(r"^In\[[0-9]*\]$", @__FILE__) != nothing

并在 VSCode 中:

所以如果你想找到VSCode,你可以检查文件是否以“.ipynb”结尾。此外:如果您从 VSCode.

运行,isdefined(Main, :VSCodeServer) 会产生 true