且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Julia 脚本中,你能判断脚本是被导入还是直接执行的吗?

更新时间:2023-12-04 22:20:52

官方 Julia 文档建议:

Official Julia doc suggests this:

if abspath(PROGRAM_FILE) == @__FILE__

    # do something only this file is executed. 
    do_something()

end

do_something函数只在代码执行时执行,从其他代码导入代码时不执行.

The do_something function is only executed when the code is executed, not when the code is imported from other codes.

Ref:如何检查当前文件是否作为主脚本运行?"https://docs.julialang.org/en/v1/manual/faq/#How-do-I-check-if-the-current-file-is-作为主脚本运行?-1

Ref:"How do I check if the current file is being run as the main script?" https://docs.julialang.org/en/v1/manual/faq/#How-do-I-check-if-the-current-file-is-being-run-as-the-main-script?-1