Search code examples
juliausing

A python "as" equivalent in julia


When I want to import a package in python, I can alias it:

import package_with_a_very_long_nameeeeee as pl

After that statement, I can refer to the package by it alias:

pl.foo()

julia allows me to do:

using PackageWithAVeryLongName
pl = PackageWithAVeryLongName
pl.foo()

But it feels like an ugly hack, possibly with implications I do not understand.

Is there an idiomatic way to alias imported packages in julia?


Solution

  • This is now possible on the upcoming Julia 1.6 using the exact same syntax as Python uses:

    julia> import LinearAlgebra as LA
    
    julia> typeof(LA)
    Module
    
    help?> LA.svd
      svd(A; full::Bool = false, alg::Algorithm = default_svd_alg(A)) -> SVD
    

    On prior versions, you can do what @Bill suggests — but I'd strongly recommend doing so as a const assignment alongside an import:

    julia> import SparseArrays
    
    julia> const SA = SparseArrays
    SparseArrays