代码之家  ›  专栏  ›  技术社区  ›  schrödingcöder

如何在Julia 0.7中包含来自不同目录的文件?

  •  0
  • schrödingcöder  · 技术社区  · 7 年前

    在学习Julia时,我偶然发现了一个基本任务,即包含来自不同目录的文件。我有两个目录, 核心 ,两者均不是另一方的母公司;请参见图。目录 核心 有档案 具有

      module Core
        foo() = 1
      end
    

    或者它可以是一个基本的 .jl 文件,而不是模块,具有 foo() .

    Directory structure

    档案 ,在目录中 ,需要使用 foo() 从…起 . 所以我试着把下面几行放进 Explore.jl : /Users/me/PycharmProjects/juliaProjects/src/Core/Core.jl , include("./Core.jl") 和其他排列。然而,我不断地犯这样的错误

     ERROR: LoadError: could not open file    /Users/me/PycharmProjects/juliaProjects/src/Core/Core.jl
     Stacktrace:
    [1] include at ./boot.jl:317 [inlined]
    [2] include_relative(::Module, ::String) at ./loading.jl:1038
    [3] include(::Module, ::String) at ./sysimg.jl:29
    [4] include(::String) at ./client.jl:398
    [5] top-level scope at none:0
    [6] include at ./boot.jl:317 [inlined]
    [7] include_relative(::Module, ::String) at ./loading.jl:1038
    [8] include(::Module, ::String) at ./sysimg.jl:29
    [9] exec_options(::Base.JLOptions) at ./client.jl:239
    [10] _start() at ./client.jl:432
    in expression starting at /Users
    

    什么是正确的答案 include jl。

    在Mac OS High Sierra上的环境是Julia 0.7。请注意,我使用的不是shell,而是IDE,即Intellij 2018.3的julia插件0.3.3。

    我查了其他问题,如 loading a module from the local directory in Julia How to import custom module in julia , What should path be to Julia source file? ,但它们似乎不起作用。

    编辑: 根据接受的答案,文件 Explore.jl 应包括

    cd("src/Core")
    include("../Core/Core.jl")
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Przemyslaw Szufel    7 年前

    不能为模块命名 Core

    请参阅下面的示例julia会话,以了解如何创建和加载位于不同文件夹中的模块(请不要更改 julia> shell> 按可获得提示 ; 退出 退格 ):

    shell> mkdir mymod
    
    shell> vim mymod/mod.jl
    
    shell> more mymod/mod.jl
    module MyMod
    foo() = 1
    end
    
    shell> mkdir dir2
    
    julia> cd("dir2")
    
    julia> include("../mymod/mod.jl")
    Main.MyMod
    
    julia> using Main.MyMod
    
    julia> MyMod.foo()
    1
    
    推荐文章