0
votes

While learning Julia, I've stumbled on a basic task of including files from a different directory. I have two directories, Core and Explore, neither of which is the parent of the other; see the figure. Directory Core has a file Core.jl with

  module Core
    foo() = 1
  end

or it could be a basic .jl file, not a module, with foo().

Directory structure

The file Explore.jl, in directory Explore, needs to use foo() from Core.jl. So I tried putting the following lines into Explore.jl: /Users/me/PycharmProjects/juliaProjects/src/Core/Core.jl, include("./Core.jl") and other permutations. However I keep getting errors like

 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

Question: What would be a correct include specification to put into a jl. file?

The environment is Julia 0.7 on Mac OS High Sierra. Note that I'm not using a shell, but an IDE, namely, the julia plug-in 0.3.3 for Intellij 2018.3.

I've checked other questions on SO, eg loading a module from the local directory in Julia, How to import custom module in julia, What should path be to Julia source file?, but they didn't seem to work.

Edit: Based on the accepted answer, the file Explore.jl should include

cd("src/Core")
include("../Core/Core.jl")
1

1 Answers

1
votes

You cannot name a module Core because it is a reserved name and (have tested on my computer) creating such module crashes Julia in unexpected way.

Please see the example julia session below to understand how to create and load a module located in a different folder (please not that changing from julia> to shell> prompt is acquired by pressing ; and exiting shell> is with backspace):

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