How to include my module file as a package?

I have some user-defined structures and I put them in Module file.

module name_module
# here all structures
end

I want to import my structures from my module as below (as a package), however, I have an error. Any idea to solve that?

using .name_module
ERROR: LoadError: UndefVarError: name_module not defined
julia> include("Module.jl")

julia> using .name_module

Oops, I didn’t look at the error you were getting carefully enough. We probably need more info on how name_module is using ModuleClasses to be able to help.

1 Like

@StevenWhitaker thanks!
Yes, I was doing this. But as you know to get ride of the compilation time of the code, I have to run it twice. So, when I run it for the second time, it raises a warning of replacing the module due to the include("name_module.jl"). So, I want to get ride of this issue?

Running include("name_module.jl") shouldn’t give an error the second time unless it threw an error the first time if I understand correctly; replacing the module will issue a warning, but not an error. There is something else happening, related to ModuleClasses based on the error message you posted above. It would probably help to post a minimum working example (MWE) to illustrate how you are getting your error.

@StevenWhitaker Thanks!

Yes, right! I am having a warning (not error) of replacing the module. Any way to avoid it?

You will always get a warning whenever replacing a module.

I’m confused as to what help you want in this post. Your original post shows an error about ModuleClasses not being defined, but now you are consistently asking for help avoiding a warning about replacing a module. These two issues seem unrelated to each other.

Again, it would probably help to include a MWE.

2 Likes

I am sorry if two things are missed up. My code is as below. I run it by clicking Execute active file in REPL via VS Code. For the first run, it is ok, but when I run the code again it shows the warning of replacing the module. So, here what is the modifications that I need to do on the code to avoid this warning?

include("name_module.jl");
using .name_module
# rest of the code

If you want to avoid the warning, you can’t include your module again. So you should run include("name_module.jl") once in the active Julia session (not in a script that you will run over and over).

1 Like

You could try FromFile ?

]add FromFile
@from "name_module.jl" using name_module
4 Likes

It gave me warnings and errors related to my structures, which were not shown before

I would just create an actual package (you don’t have to register it publicly); it’s the best way to organize and re-use any nontrivial amount of code in the long run. See also

(With your code in a package, use Revise.jl — then your files will automatically be re-loaded as needed when you edit the files during development.)

7 Likes

@Amro:
To start off, I should first mention my agreement with @stevengj 's previous post. It is highly likely that you would benefit overall by creating an actual package to house your new code.

Having said that, I noticed there being much confusion regarding the differences between include and using/import. I think it is worth explaining these differences so you can avoid pitfalls like the one your issue has hilighted.

include(): What does it do?

Applying terminology typical programmers seem more familiar with: we could almost say that:
→ include("some_file_with_code.jl")
causes Julia to “compile” some_file_with_code.jl — inline with said call (i.e. at the location/time the compiler reaches your call to include()).

Sidenote:

However, if I am to use more accurate terminology, I believe you should say that the Julia parser “parses” that file, and performs code lowering in order to store various symbols, modules, functions, etc. into a complex tree of data structures… Because what is technically known as the “compilation process” actually happens at a later time.

Point being, the code you include() is available for use immediately after the call. Upon inclu-sion, Julia generates the appropriate symbols, and makes them available to your current scope…

And if you are wondering, calling:
julia> include("some_file_with_code.jl")
from the Julia prompt makes the newly “lowered” code available in the module named >>Main<<.

using: What does it do?

In the case where we are dealing with pure modules (not full-blown packages), using SomeModuleSymbol gets Julia to import a symbol (here: your module’s name) into the current namespace.

Note that it would also automatically import any symbols that have been marked for exporting using “export statements” (had you included any inside your module).

“Ok. So what’s wrong with my code?” (paraphrasing)

First off, when you: include("name_module.jl"), Julia does the whole code-lowering thing, and makes “name_module” available in your namespace.

Then, when you call:

using .name_module

Julia tries to re-import “name_module” that is already available in said namespace.

So what do I do to fix this?

There is simply no need for the “using .name_module” command in your example.

Instead, you can immediately start using the module that was made available by calling “include("name_module.jl")”, as done here:

include("name_module.jl");
#using .name_module #Nope: Will re-import already existing symbol

#Just start using the module that is now available:
name_module.run_something_inside_module()

# rest of the code
1 Like

Thanks for your explanation. I am using using .name_module to allow using the functions in the module directly without writing name_module. before the functions. Can I do that also without writing using .name_module?

Probably not. You can however use a nickname, if this helps you in any way.

import .name_module
const M = name_module
M.my_function(...)
1 Like

Thanks for your reply. So, here eventually do I need also to put include("name_module.jl"); because this is a user-defined model as below?

include("name_module.jl");
import .name_module
const M = name_module
M.my_function(...)

If it is in a file called name_module.jl, then yes, but you can define as many modules inside another modules, as many you want per file.

Just note that you should include every file just once in your whole project/package, and then if multiple of your modules need the included module then they should import (or call using) passing the relative path in the module hierarchy (not the relative path in the filesystem).

1 Like

Thank you very much. Can you give me an example here?

See this previous recent thread: Referencing the same module from multiple files

1 Like

A possible quick-and-dirty fix ?

if !@isdefined name_module; include("name_module.jl"); using .name_module;end
1 Like

Thank you very much. I will try it.