# Is there a way to import modules with a string?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 1, 2018, 7:54am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723 "2018-10-01T07:54:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 7:54am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/1 "2018-10-01T07:54:47Z")

</div>

```julia
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?

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 1, 2018, 8:08am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/2 "2018-10-01T08:08:10Z")

</div>

Checkout Requires.jl.

```julia
m = :SomeMod
@eval import .$m

```

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 8:22am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/3 "2018-10-01T08:22:35Z")

</div>

Thank you! 😃

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 1, 2018, 8:33am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/4 "2018-10-01T08:33:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 8:35am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/5 "2018-10-01T08:35:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 8:43am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/6 "2018-10-01T08:43:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 1, 2018, 9:27am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/7 "2018-10-01T09:27:11Z")

</div>

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

```julia
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
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                                                                                                                                                                  

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 1, 2018, 9:28am UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/8 "2018-10-01T09:28:08Z")

</div>

```julia
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.

---

<div class="post-metadata">

**Author:** ![ErwanPiR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erwanpir/32/11434_2.png) [@ErwanPiR](https://discourse.julialang.org/u/ErwanPiR)\
**Post date:** [March 1, 2019, 3:15pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-import-modules-with-a-string/15723/9 "2019-03-01T15:15:05Z")

</div>

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.
