从 Julia 1.0 中特定目录中的文件导入模块和函数 [英] Import modules and functions from a file in a specific directory in Julia 1.0
问题描述
假设我有一个文件 File.jl
有一个模块 MyModule
包含函数 foo
和 bar
在里面.在与模块文件相同的目录中,我有一个脚本 Script.jl
,我想在脚本中使用 MyModule
中的函数.
Let's say I had a file File.jl
that had a module MyModule
containing the functions foo
and bar
in it. In the same directory as the module-file, I had a script Script.jl
, and I wanted to use the functions in MyModule
in the script.
要怎么做呢?
推荐答案
为了找到不在标准 LOAD_PATH 中的模块并能够导入它们,您需要显式更新当前文件夹的 LOAD_PATH 变量
In order to find Modules that are not in the standard LOAD_PATH and be able to import them, you need to update your LOAD_PATH variable for the current folder explicitly
push!( LOAD_PATH, "./" )
然后您将能够适当地导入模块.
then you will be able to import a module appropriately.
请注意,如果文件名为 File.jl
并定义了模块 MyModule
,那么您应该导入的是 import MyModule
,不是导入文件
.在这种情况下,一般建议您使用与定义模块相同的文件名,以避免混淆.
Note that, if the file is called File.jl
and defines the module MyModule
, what you should be importing is import MyModule
, not import File
. It is generally recommended you use the same name for the file as for the defined module in this kind of scenario, to avoid confusion.
还请注意,正如上面提到的@crstnbr,您还可以通过简单地包含"文件的内容来简单地将文件的内容转储"到当前会话中;但是请注意,这只是在现场创建模块,因此任何预编译指令等都不会得到尊重.
Also note, As @crstnbr noted above, you can also simply 'dump' the file's contents into the current session by simply 'including' it; note however that this simply creates the module on the spot, so any precompilation directives etc will not be honoured.
一些相关的问题/答案(免责声明:由我提供)您可能会觉得有用:
Somewhat related questions / answers (disclaimer: by me) you might find useful:
这篇关于从 Julia 1.0 中特定目录中的文件导入模块和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!