Importing Functions from module

I’m quite confused about what exactly a module is. As I understand it a module is just a collection of functions that I can import, similar to a package. So, I have four functions that I want to import that I have put into a module, as below, in a file called DVHMetric.jl

module DVHMetric
using SpecialFunctions: erf

        function probit(x) #Probit function
                return 0.5*(1+erf(x/sqrt(2)))
        end


        function EUD(D,V,n) #EUD summation
                return sum(V.*(D.^(1/n))).^n
        end

        function NTCP(EUD,D_50,m)
                probit((EUD-D_50)/(m*D_50))
        end

        function TCP(D,V,D_50,γ_50)
                (1/2)^(sum(V.*exp(2*γ_50*(1-D/d_50)/log(2))))
        end # function
export probit, EUD, NTCP, TCP
end

If I understand correctly, the code above writes four functions and specifies that those four functions are what is contained in the module (in the export line).

Now, in a separate script, I want to import this module and call its functions using their given names. So I have tried the following in a script that is saved in the same directory as the module.

include("DVHMetric.jl")
probit(2)

However I get an error that probit is not defined. I have also tried import DVHMetric and using DVHMetric both of which return errors saying “Package DVHMetric not found in current path”.

So, how do I import this module and use its functions as I have named them? I was able to call the functions using the syntax below without issue

include("DVHMetric.jl")
DVHMetric.probit(2)

but I want to use the previous syntax where I just call probit.

You need to load the module with using (or import), see https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage-1.

Since you don’t define a package, just a module that you include, you need a . in front, so probably something like

include("CVHMetric.jl")
using .DVHMetric

You can also set it up like a proper package and load it with just using DVHMetric.

1 Like

Thank you! the . was the issue in my case. I am not quite sure how to set it up like a proper package so this will do for now :slight_smile: