Cant use module normally

In This tutorial they even have an online code executer in which they set the example of using a module. They first have a file mymodule.jl in which they write:

`module MyModule

export sum_function, resta

function sum_function(a, b)
return a + b
end
function resta(a,b)
a-b
end
end # end of module definition`

And a main.jl file in which they write:

include("mymodule.jl")
using MyModule

result = resta(2, 3)

print(result)

When I try to mimic this same structure in my files, I have a file called cont_stoch_proc.jl in which I have a toy example:

module ContStochProc
export f

function f(x, y,z...) 
    value=x*y
    for i in z
        value+=i
    end 
    return value
end
end # module ContStochProc

And a file called prueba.jl in which I have:

include("cont_stoch_proc.jl")
using ContStochProc
f(2,3,1,1,1,1,1)

But when I run it, it shows me an error, signaling I do not have that module in my path

ERROR: ArgumentError: Package ContStochProc not found in current path, maybe you meant `import/using .ContStochProc`.

First question would be why does it work on the web but not in my project? I also realized that it works only if I write

using Main.ContStochProc

So why only in my project I need to use the “Main.” module path to run it? (this also happens if I try to use import ContStochProc.f as is used in the example

The tutorial is wrong, I’m not sure if it’s just using an older version of Julia. To use a locally defined module you would generally use using .ContStochProc. Per the manual: “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.”

4 Likes

I just want to add, that the error hint already tells you the right answer. :smiley:

2 Likes

Thank you. I also found that after making the question but knowing the tutorial is not the way it should work really helps.

1 Like

The author’s other writings seem to be generated text due to a particular redundant format and self-contradicting mistakes so close that a human wouldn’t make them, e.g. 13 print(x is y) # Output: True followed by the “Explanation” list saying “Line 13: We again check if x and y refer to the same object in memory. Since both variables now contain the value 300, which is out of the range -5 to 256, the output will be False.” The languages and topics are also all over the place, really casting doubt on any human making a tutorial with any purpose.

There’s lots of junk on the internet now, it’s tough for people trying to find tutorials organized to be more accessible than the Manual.

2 Likes