# UndefVarError: Distributions not defined

**URL:** <https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428>\
**Category:** New to Julia\
**Tags:** question, error, distributions\
**Created:** [July 21, 2020, 12:25pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428 "2020-07-21T12:25:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juby\_Ruby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juby_ruby/32/10914_2.png) [@Juby\_Ruby](https://discourse.julialang.org/u/Juby_Ruby)\
**Post date:** [July 21, 2020, 12:25pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/1 "2020-07-21T12:25:13Z")

</div>

Hi,  
I am very new to Julia but need it for numerical simulations for my master thesis.  
I am using the Distributions package, since I have to sample from a von Mises Distribution with parameter \kappa.  
Generally sampling from a von Mises distribution works, however in my code I have to do that within a function, since the parameter \kappa depends on other variables defined in the function’s scope.  
Unfortunately I cannot get that to work, and I always get an “UndefVarError: Distributions not defined” error.  
I assume there is some issue with the scope of variables, however I have no idea how to get the code to work.

Here is the function, where I want to sample from a von Mises distribution (var.n\_label[i,j] is a boolean variable, defined in another function above this one):

```julia
function set_neighbour_orientation(param,var)
     for i=1:param.N
         tmpx = tmpy = 0.0
         for j=1:param.N 
             tmpx += var.n_label[i,j] * cos(var.θ[j])
             tmpy += var.n_label[i,j] * sin(var.θ[j]) 
         end
         avg_tmpx = tmpx/param.N 
         avg_tmpy = tmpy/param.N #(define the flux)
         J_i =(avg_tmpx, avg_tmpy)
         square = avg_tmpx * avg_tmpx + avg_tmpy * avg_tmpy
         κ = sqrt(square)
         vm = Distributions.VonMises(κ)
         var.ψ[i] = rand(vm)
     end
 end

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 21, 2020, 12:33pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/2 "2020-07-21T12:33:28Z")

</div>

At the risk of suggesting something trivial, have you checked that you’ve definitely run `using Distributions` before calling the function?

---

<div class="post-metadata">

**Author:** ![Juby\_Ruby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juby_ruby/32/10914_2.png) [@Juby\_Ruby](https://discourse.julialang.org/u/Juby_Ruby)\
**Post date:** [July 21, 2020, 12:39pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/3 "2020-07-21T12:39:34Z")

</div>

Yeah I am pretty sure of that. If I put something like

```julia
vm=VonMises()
v= rand(vm)
println(v)

```

anywhere else in the code, outside of a function, it prints some von Mises sample and throws the error after that.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 21, 2020, 12:45pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/4 "2020-07-21T12:45:51Z")

</div>

Is your function part of a module?  
Or: How do you run/define your function? Copy/Paste into REPL? include? using yourModule?

---

<div class="post-metadata">

**Author:** ![Juby\_Ruby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juby_ruby/32/10914_2.png) [@Juby\_Ruby](https://discourse.julialang.org/u/Juby_Ruby)\
**Post date:** [July 21, 2020, 12:56pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/5 "2020-07-21T12:56:55Z")

</div>

Yes it is part of a module and I define the functions within that module like that:

```julia
import .mod_vicsek_model:  
set_initial_condition,
set_neighbour_list,
set_neighbour_orientation,
set_white_noise,
set_new_θ,
set_new_r,
set_periodic_bc,
set_new_rθ

```

It’s very much possible that my mistake is somewhere in how I call modules/ functions, since I am not very accustomed to the julia syntax.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 21, 2020, 12:59pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/6 "2020-07-21T12:59:42Z")

</div>

You need  
`using Distributions`  
inside your module, before importing the function:  
e.g.:

```julia
module mod_vicsek_model
using Distributions
...
function set_neighbour_orientation(param,var)
         ...
         vm = VonMises(κ)
         ...
     end
 end
...
end

```

Than you can just call

```julia
VonMises()

```

without `Distributions.` at front.  
(edited to fit more your given information)

[https://docs.julialang.org/en/v1/manual/modules/](https://docs.julialang.org/en/v1/manual/modules/)

---

<div class="post-metadata">

**Author:** ![Juby\_Ruby](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juby_ruby/32/10914_2.png) [@Juby\_Ruby](https://discourse.julialang.org/u/Juby_Ruby)\
**Post date:** [July 21, 2020, 3:01pm UTC](https://discourse.julialang.org/t/undefvarerror-distributions-not-defined/43428/7 "2020-07-21T15:01:52Z")

</div>

Thank you, this solved my issue!
