从Julia 1.0中特定目录中的文件导入模块和功能 [英] Import modules and functions from a file in a specific directory in Julia 1.0

查看:298
本文介绍了从Julia 1.0中特定目录中的文件导入模块和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件File.jl,该文件的模块MyModule中包含功能foobar.在与模块文件相同的目录中,我有一个脚本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,而不是import File.在这种情况下,通常建议您为文件使用与定义的模块相同的名称,以免造成混淆.

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:

  • https://stackoverflow.com/a/50627721/4183191
  • https://stackoverflow.com/a/49405645/4183191

这篇关于从Julia 1.0中特定目录中的文件导入模块和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆