Difference between include, use and import

I read about include, use and import in Julia.
It seems they can all be used to ‘import’ a module.

But I am still not very clear the difference between include, use and import.
if I have a module called Mymod, and I want to use it in the main program,
is
include(“Mymod.jl”) enough?

Or do I have to do,
include(“Mymod.jl”)
use .Mymod

Also, could anyone please explain a little bit more,
so, when to use ‘use’, and when to use ‘import’?

Thanks in advance!

Yes, you need to do that, unless it is a package and is installed as such.

For the differences between using and import, the manual is very didactical: Modules · The Julia Language

3 Likes

What have you read? What exactly are you unclear about?

include is used for including the content of a file into your current program. It has per se nothing to do with modules or namespaces. using and import on the other hand have nothing to do with files, and deal exclusively with modules/namespaces.

3 Likes

Thank you I got it.

If I have some variable or function in the export list in the module NiceStuff,
like

export Superman

then I do

using NiceStuff

I can use Superman directly.

In contrast,

    import NiceStuff

brings only the module name into scope.
Therefore Superman alone does not work, I have to specify NiceStuff.Superman all the time.

Yes, that’s the difference between using and import - the former brings all exported names into scope, while the latter only brings NiceStuff (the module identifier) into scope.

https://docs.julialang.org/en/v1/manual/modules/#Standalone-using-and-import