Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import custom module in julia

Tags:

julia

I have a module I wrote here:

# Hello.jl module Hello     function foo         return 1     end end 

and

# Main.jl using Hello foo() 

When I run the Main module:

$ julia ./Main.jl 

I get this error:

ERROR: LoadError: ArgumentError: Hello not found in path  in require at ./loading.jl:249  in include at ./boot.jl:261  in include_from_node1 at ./loading.jl:320  in process_options at ./client.jl:280  in _start at ./client.jl:378 while loading /Main.jl, in expression starting on line 1 
like image 521
dopatraman Avatar asked May 13 '16 01:05

dopatraman


People also ask

How do I add a module to Julia?

Installing modulesEdit To download and install a package, you can use Julia's package manager, Pkg. Start by typing a right bracket ] into the REPL to enter the package manager, then use the add command. You don't have to use quotes or the ". jl" suffix.

How do you call a module in Julia?

To load a module from a package, the statement using ModuleName can be used. To load a module from a locally defined module, a dot needs to be added before the module name like using . ModuleName . would load the above code, making NiceStuff (the module name), DOG and nice available.

What is the difference between import and using in Julia?

My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.


1 Answers

There is a new answer to this question since the release of Julia v0.7 and v1.0 that is slightly different. I just had to do this so I figured I'd post my findings here.

As already explained in other solutions, it is necessary to include the relevant script which defines the module. However, since the custom module is not a package, it cannot be loaded as a package with the same using or import commands as could be done in older Julia versions.

So the Main.jl script would be written with a relative import like this:

include("./Hello.jl") using .Hello foo() 

I found this explained simply in Stefan Karpinski's discourse comment on a similar question. As he describes, the situation can also get more elaborate when dealing with submodules. The documentation section on module paths is also a good reference.

like image 192
kiliantics Avatar answered Nov 21 '22 08:11

kiliantics