5

There are other questions related to this issue on SO but I think I tried the recommended solution without success though.

So, in the spirit of my baby-steps towards learning Julia, I tried to create a module locally. The folder structure of my module is as follows:

- TrimmedGrassmanAvwerage/
    - src/
        - TrimmedGrassmanAverage.jl
        - Utils/
            - dummy.jl

The contents of dummy.jl are simply:

function dummy(x)
    x + oneunit(x)
end

Similarly, the contents of TrimmedGrassmanAverage are:

module TrimmedGrassmanAverage

export Utils,
       dummy

include("Utils/dummy.jl")

end

Now, in my REPL I do the following:

julia> include("TrimmedGrassmanAverage/src/TrimmedGrassmanAverage.jl")

This returns:

Main.TrimmedGrassmanAverage

Now I do:

using TrimmedGrassmanAverage

This returns the error:

ERROR: ArgumentError: Package TrimmedGrassmanAverage not found in current   path:
- Run `Pkg.add("TrimmedGrassmanAverage")` to install the TrimmedGrassmanAverage package.

I thought that doing the `include("...") will put the package in the path and julia should be able to find it. However, clearly there is some more steps missing.

EDIT

However, I can do:

using Main.TrimmedGrassmanAverage

and then I can call:

TrimmedGrassmanAverage.dummy(10);

I am not sure why the Main namespace has to be added and also why I cannot just call dummy without the Trimmed... since I export the dummy function.

Luca
  • 10,458
  • 24
  • 107
  • 234

1 Answers1

4

How to load the module. Use either:

using Main.TrimmedGrassmanAverage

or

using .TrimmedGrassmanAverage

which will have the same effect. The reason is that when you run include your module gets defined within Main module scope. using Something does look for a package (and you have defined a module in a file only). Package lookup is governed by LOAD_PATH and DEPOT_PATH variables (see https://docs.julialang.org/en/latest/stdlib/Pkg/#Glossary-1).

EDIT: it actually seems that dummy is exported properly on Julia 1.0

julia> module TrimmedGrassmanAverage

export Utils,
       dummy

function dummy(x)
    x + oneunit(x)
end
end
Main.TrimmedGrassmanAverage

julia> using Main.TrimmedGrassmanAverage

julia> dummy(10)
11

at least on my Julia.

A side comment to your code is that Utils is not defined in the module and you try to export it (note that in Julia it does not matter from where you take the file that you include - this function works like copy-paste of code).

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Thanks for replying! Ahhhh ok, this clarifies thing for me. very nice. – Luca Sep 26 '18 at 14:48
  • 1
    Actually I have checked that `dummy` is exported properly even if `Utils` is undefined (see my copy-paste in the answer). Can you double check that it does not work in your Julia. If yes please let me know. – Bogumił Kamiński Sep 26 '18 at 14:49
  • 1
    I have added some comments about package lookup mechanics that might be also relevant for you in the future. – Bogumił Kamiński Sep 26 '18 at 14:57