3

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.

How would one go about doing this?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
sesodesa
  • 1,473
  • 2
  • 15
  • 24

2 Answers2

3

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.

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.

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:

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • Can the push!-function be run from within the script in a way that the change in the LOAD_PATH doesn't become permanent? – sesodesa Aug 14 '18 at 07:14
  • Regardless, this seems to have solved the import issue, but now I receive the following error message: `LoadError: importing MyModule into Main conflicts with an existing identifier`. It's caused by the line that imports the module by calling `using MyModule`. – sesodesa Aug 14 '18 at 07:21
  • Your module seems to be exporting a function that already exists in Main (maybe because another package exports the same function) – carstenbauer Aug 14 '18 at 07:27
  • @crstnrb Is this an issue with `using`? Shoud I be using `import` instead, if my module exports anything? – sesodesa Aug 14 '18 at 07:34
  • @TheSodesa yes, just use import and use the exported function qualified by module name. See [my other answer here](https://stackoverflow.com/a/39566646/4183191) on this as well. – Tasos Papastylianou Aug 14 '18 at 09:32
  • @TheSodesa also, to answer your first question, the LOAD_PATH variable is not saved after you end each session, so it's safe to `push!` stuff to it. If you __did__ want the LOAD_PATH changed permanently, you'd do so by adding such `push!` instructions in your julia initilization script. – Tasos Papastylianou Aug 14 '18 at 09:33
2

You include the file with the module definition and call the functions in your script file:

include(joinpath(@__DIR__,"File.jl"))
MyModule.foo()
MyModule.bar()
# or just foor() and bar() if MyModule exports those functions

The @__DIR__ expands to the directory of the script file, see

help?> @__DIR__
  @__DIR__ -> AbstractString

  Expand to a string with the absolute path to the directory of the file containing the macrocall. Return the current working directory if run from a REPL or if evaluated by julia -e <expr>.
carstenbauer
  • 9,817
  • 1
  • 27
  • 40