# How to use units in Cosmology.jl

**URL:** <https://discourse.julialang.org/t/how-to-use-units-in-cosmology-jl/51935>\
**Category:** New to Julia\
**Tags:** unitful\
**Created:** [December 16, 2020, 2:49pm UTC](https://discourse.julialang.org/t/how-to-use-units-in-cosmology-jl/51935 "2020-12-16T14:49:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [December 16, 2020, 2:49pm UTC](https://discourse.julialang.org/t/how-to-use-units-in-cosmology-jl/51935/1 "2020-12-16T14:49:06Z")

</div>

I have installed and now am trying to use the Cosmology.jl package.

When I try, from a recently launched Julia REPL:

```julia
using Cosmology
c = cosmology()
hubble_dist(c,0)

```

I get the expected output: 434.818231884059 Mpc. However, right after, when I try:

```julia
hubble_dist(km, c, 0)
hubble_dist(u"km", c, 0)
hubble_dist(Gpc,c,0)
hubble_dist(u"Gpc", c, 0)

```

there is always an error with UndefVarError, either with km or Gpc not defined or rather @u\_str not defined. I simply could not manage to use distinct units from the default ones. What am I doing wrong?  
Thanks

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 16, 2020, 3:18pm UTC](https://discourse.julialang.org/t/how-to-use-units-in-cosmology-jl/51935/2 "2020-12-16T15:18:12Z")

</div>

I think you’ll need to explicitly load Unitful.jl, which provides the `@u_str` macro used in your examples, as well as UnitfulAstro, which provides `Gpc` and other astro-relevant units.

```julia
julia> using Cosmology, Unitful, UnitfulAstro

julia> c = cosmology()
Cosmology.FlatLCDM{Float64}(0.69, 0.7099122024007928, 0.29, 8.779759920715362e-5)

julia> hubble_dist(u"km", c, 0)
1.3406708213779601e23 km

julia> hubble_dist(u"Gpc", c, 0)
4.34481823188406 Gpc

```

You can also import the units from the internals of Cosmology.jl, but there are no guarantees of stability when using a package’s internals.

```julia
julia> import Cosmology: km, Mpc

julia> hubble_dist(km, c, 0)
1.3406708213779598e23 km

```

I opened an [issue](https://github.com/JuliaAstro/Cosmology.jl/issues/37) to help clarify this.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [December 16, 2020, 3:42pm UTC](https://discourse.julialang.org/t/how-to-use-units-in-cosmology-jl/51935/3 "2020-12-16T15:42:06Z")

</div>

@stillyslalom Thank you; it worked like a charm, now.
