7

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?

Peter Staab
  • 540
  • 3
  • 11
  • Possible duplicate of [How to import custom module in julia](https://stackoverflow.com/questions/37200025/how-to-import-custom-module-in-julia) – kiliantics Oct 29 '18 at 01:06

1 Answers1

10

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.

Harrison Grodin
  • 2,253
  • 2
  • 19
  • 30
  • 1
    Hi, sorry for bringing up old thread. I'm assuming I suppose to run this after `include("mymodule.jl")`? How can I use `MyModule` without `include`? The thing is there are functions I don't want to export and by including it, I can still access it with `Main.MyModule.function_I_dont_want_to_import`. – Darren Christopher Sep 24 '19 at 23:55