Hi, I’m a bit unsure about what would be the best way to import a module with
using. I understand, looking throught past julia topics, that if you want to import a local module, let’s say Users.jl, you should do
include(Users.jl) and then you can do
using .Users to get it back and indeed it works. However I saw, again in a past julia topic, that if “you wish to access the features of a Julia package within your code, you must either call
using (as opposed to
include)”. I indeed only want to use features of module however if I don’t
include it beforehand I have an error saying that Users is undefined. I tried to
push! to LOAD_PATH the path of the folder in which the Users.jl module is defined but it did not work and anyway as I rade it doesn’t seem like it’s an ideal solution… In my real case scenario I have a lot of modules, each one being defined inside different folders. LOAD_PATH seemed initially an ideal way as I thought I wouldn’t have to include one by one all my modules and would just be able to push to it the top level folder containing every subfolders, and then be able to access them every where in the code by doing
using myModule. But did not work… Right now the code is working but if you have any idea of what the ideal way to proceed would be, I would really appreciate it.
This is my code structure :
This is my code :
#main.jl cd(@__DIR__) function main() include(joinpath("app", "App.jl")) Base.eval(Main, :(using Revise)) end; main() using App.Users using Revise #App.jl module App function main() include(joinpath("config", "config.jl")) end; main() end #config.jl include(joinpath("..", "resources","users","Users.jl")) #This is what I tried with the LOAD_PATH variable # const ROOT_PATH = pwd() # LOAD_PATH = ["@", "@v#.#", "@stdlib", ROOT_PATH] # appPath = joinpath(ROOT_PATH,"app") # resourcesPath = joinpath(appPath,"resources") # usersPath = joinpath(resourcesPath,"users") # LOAD_PATH = ["@", "@v#.#", "@stdlib",ROOT_PATH, appPath, resourcesPath, usersPath] #println(LOAD_PATH) include(joinpath("..","services","service1.jl")) using .Users #Users.jl module Users export User mutable struct User id::Integer name :: String end end #service1.jl using .Users myUser = User(123,"agathe") println(myUser)
There is different
using a bit every where in the code to help me understand how to import module depending on where I am but the main goal is to use in services files, modules defined in the resources folder (many modules in real case scenario) , and to include this services files (many files in real case scenario) into the config.jl file. Furthermore this config.jl file is called from an App.jl file via
include. By mimetism (from other projects) I turned this App.jl into a module that is called from a main.jl file but I think I don’t really get the advantages of doing so… what do I get from my App being a module ? Finally last small precision, I run this code from julia REPL by writing
My apologies in advance if my questions are unclear or if I put too many questions in one topic, don’t hesitate to tell me, I will do better next time. Thank you !