Is there a way to import modules with a string?

question
#1
moduleName = "Name"

# Is there a possibility to import a module based on that variable? something like
import ."$moduleName" # This is invalid but is there a possibility to do such a thing WITHOUT using include?
0 Likes

#2

Checkout Requires.jl.

m = :SomeMod
@eval import .$m
0 Likes

#3

Thank you! :smiley:

0 Likes

#4

You’re welcome! There should be an option to mark it as solved. Saves other people some time.

0 Likes

#5

I got an error when trying to import other module in different file by doing
@eval import ..$module
It says ..$ is an invalid operator

0 Likes

#6

Okay I got an issue, if the string is dynamic how would I use the module?
eventName = content |> Symbol
@eval import .$eventName
Now how would I access this module functions if I don’t know it’s name?

0 Likes

#7

This all sounds a bit iffy. Maybe you should try a different approach to your problem. Anyway, something like

m = :SomeMod
@eval import .$m
@eval const Mod = $m
Mod.fun(1)

The @eval import ..$m doesn’t seem to parse, so you have to construct the Expr by hand.
So all in all:

julia> module A
       fun(x) = x
       end
Main.A
                                                                                                                                                                        
julia> module B
       m = :A
       eval(Expr(:import, (Expr(:., :., :., :A))))
       @eval const Mod = $m
       @show Mod.fun(1)
       end                                                                                                                                                                                                                                                                                                       
Mod.fun(1) = 1                                                                                                                                                          
Main.B                                                                                                                                                                  
0 Likes

#8
julia> foo = Symbol("LinearAlgebra")
:LinearAlgebra

julia> @eval using $foo

julia> getfield(getfield(Main, foo), :mul!)
mul! (generic function with 122 methods)

Please do tell us what you’re actually trying to do though, this level of dynamism is usually not required.

2 Likes

#9

Hi,

I’m indeed curious if there is another way to do that. My problem is similar, I would like to import a list of modules listed in a file (JSON).
Obviously when I read the file I get a list of strings. From this list of strings I would like to import the modules and use the functions inside the modules. I also have just the names of the functions in a string format.

When I trie

eval( Expr(:import, (Expr(:., :., :., Symbol(“nameModule”)

I get “could not import Main.nameModule into Main”

Maybe it isn’t the right way to do it but I couldn’t think of another way to run some functions inside modules when you get the name of the modules/functions from a file (into string format).

I hope my post is clear enough…

Thanks for you ideas.

0 Likes