且构网

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

如何确定julia脚本是作为模块包含还是作为脚本运行?

更新时间:2023-12-04 22:29:04

tl; dr版本:

if !isdefined(:__init__) || Base.function_module(__init__) != MyModule
  main()
end


说明:

似乎有些混乱. Python和Julia在模块"方面的工作非常不同(即使两者使用相同的术语,但原则上它们是不同的).

There seems to be some confusion. Python and Julia work very differently in terms of their "modules" (even though the two use the same term, in principle they are different).

在python中,源文件可以是模块或脚本,具体取决于您选择加载"/运行"它的方式:样板存在,可以通过查询源文件来检测运行源代码的环境.执行时嵌入模块的__name__.例如.如果您有一个名为mymodule.py的文件,则可以正常导入该文件,然后在模块定义中将变量__name__自动设置为值mymodule;但是如果将其作为独立脚本运行(有效地将代码转储"到主"模块中),则__name__变量是全局范围的变量,即__main__.这种差异使您能够检测python文件的运行方式,因此每种情况下的行为都可能略有不同,这正是样板程序所做的.

In python, a source file is either a module or a script, depending on how you chose to "load" / "run" it: the boilerplate exists to detect the environment in which the source code was run, by querying the __name__ of the embedding module at the time of execution. E.g. if you have a file called mymodule.py, it you import it normally, then within the module definition the variable __name__ automatically gets set to the value mymodule; but if you ran it as a standalone script (effectively "dumping" the code into the "main" module), the __name__ variable is that of the global scope, namely __main__. This difference gives you the ability to detect how a python file was ran, so you could act slightly differently in each case, and this is exactly what the boilerplate does.

但是,在julia中,明确地将模块定义为代码.运行包含module声明的文件将加载该模块,而不管您是执行using还是include的.但是,在前一种情况下,如果模块已经在工作空间中,则不会重新加载该模块,而在后一种情况下,就好像您在重新定义"了该模块一样.

In julia, however, a module is defined explicitly as code. Running a file that contains a module declaration will load that module regardless of whether you did using or include; however in the former case, the module will not be reloaded if it's already on the workspace, whereas in the latter case it's as if you "redefined" it.

模块可以通过特殊的__init__()函数具有初始化代码,该函数的作用是仅在模块首次加载时运行(例如,通过using语句导入时).因此,您可以做的是拥有一个独立脚本,您可以将include直接作为独立脚本运行,也可以在module定义范围内使用include脚本,并检测模块是否存在特定的变量,以使其在每种情况下的行为都不同.但是它仍然必须是独立于主模块定义的独立文件.

Modules can have initialisation code via the special __init__() function, whose job is to only run the first time a module is loaded (e.g. when imported via a using statement). So one thing you could do is have a standalone script, which you could either include directly to run as a standalone script, or include it within the scope of a module definition, and have it detect the presence of module-specific variables such that it behaves differently in each case. But it would still have to be a standalone file, separate from the main module definition.

如果您想让模块做一些事情,而独立脚本不应该这样做,这很容易:您只有这样的东西:

If you want the module to do stuff, that the standalone script shouldn't, this is easy: you just have something like this:

module MyModule
  __init__() = # do module specific initialisation stuff here
  include("MyModule_Implementation.jl")
end

如果您想要相反的情况,则需要一种方法来检测您是否在模块内部运行.您可以这样做,例如通过检测属于该特定模块的合适的__init__()函数的存在.例如:

If you want the reverse situation, you need a way to detect whether you're running inside the module or not. You could do this, e.g. by detecting the presence of a suitable __init__() function, belonging to that particular module. For example:

### in file "MyModule.jl"
module MyModule
  export fun1, fun2;
  __init__() = print("Initialising module ...");
  include("MyModuleImplementation.jl");
end

### in file "MyModuleImplementation.jl"
fun1(a,b) = a + b;
fun2(a,b) = a * b;

main() = print("Demo of fun1 and fun2.     \n" *
               "  fun1(1,2) = $(fun1(1,2)) \n" *
               "  fun2(1,2) = $(fun2(1,2)) \n");

if !isdefined(:__init__) || Base.function_module(__init__) != MyModule
  main()
end

如果MyModule作为模块加载,则MyModuleImplementation.jl中的main函数将无法运行.

If MyModule is loaded as a module, the main function in MyModuleImplementation.jl will not run.

如果将MyModuleImplementation.jl作为独立脚本运行,则main函数将运行.

If you run MyModuleImplementation.jl as a standalone script, the main function will run.

因此,这是一种实现所需效果的方法;但这与说将模块定义文件作为模块或独立脚本运行非常不同;我认为您不能简单地从代码中剥离" module指令并以这种方式在julia中运行模块的内容".

So this is a way to achieve something close to the effect you want; but it's very different to saying running a module-defining file as either a module or a standalone script; I don't think you can simply "strip" the module instruction from the code and run the module's "contents" in such a manner in julia.