Module aliasing in Julia
Asked Answered
S

3

15

In python you can do something like this to let you use a shortened module name:

>>> import tensorflow as tf

From then on you can refer to tf, instead of having to type tensorflow everywhere.

Is something like this possible in Juila?

Stormproof answered 8/2, 2017 at 3:26 Comment(0)
W
26

Yep, you can just assign the module to a new name.

import JSON
const J = JSON

J.print(Dict("Hello, " => "World!"))

I highly recommend to use the const because otherwise there will be a performance penalty. (With the const, there is no performance penalty.)

Wreckfish answered 8/2, 2017 at 3:29 Comment(1)
See also github.com/JuliaLang/julia/issues/1255. This will probably have a better syntax at some point.Explication
T
9

Julia now supports renaming with as.

Thyroxine answered 27/5, 2021 at 11:36 Comment(0)
I
2

I'm pushing the practical snippet @Alan don't mentioned:

  • At MyMod.jl:
module MyModule

plus(x, y) = x + y
myfield = 0

end
  • At Main.jl:
# Only if you're including the module from another file.
include("MyMod.jl")

import .MyModule as mymod

println(mymod.myfield)
println(mymod.plus(1, 1))

Main resources:

Insignificancy answered 28/12, 2022 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.