Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and using a custom module in Julia

Tags:

module

julia

Although this question has been asked before, it appears that much has changed with respect to modules in Julia V1.0.

I'm trying to write a custom module and do some testing on it. From the Julia documentation on Pkg, using the dev command, there is a way to create a git tree and start working.

However, this seems like overkill at this point. I would like to just do a small local file, say mymodule.jl that would be like:

module MyModule

export f, mystruct

function f()
end

struct mystruct
  x::Integer
end

end # MyModule

It appears that it used to be possible to load it with

include("module.jl")
using MyModule

entering the include("module.jl"), it appears that the code loads, i.e. there is no error, however, using MyModule gives the error:

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

I notice that upon using include("module.jl"), there is access to the exported function and struct using the full path, MyModule.f() but I would like the shorter version, just f().

My question then is: to develop a module, do I need to use the Pkg dev command or is there a lighter weight way to do this?

like image 563
Peter Staab Avatar asked Jan 28 '23 06:01

Peter Staab


1 Answers

In order to use a local module, you must prefix the module name with a ..

using .MyModule

When using MyModule is run (without the .), Julia attempts to find a module called MyModule installed to the current Pkg environment, hence the error.

like image 171
Harrison Grodin Avatar answered Feb 07 '23 14:02

Harrison Grodin