# Invoke user defined package functions on distributed workers

**URL:** <https://discourse.julialang.org/t/invoke-user-defined-package-functions-on-distributed-workers/45388>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 22, 2020, 10:01pm UTC](https://discourse.julialang.org/t/invoke-user-defined-package-functions-on-distributed-workers/45388 "2020-08-22T22:01:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack/32/17406_2.png) [@Jack](https://discourse.julialang.org/u/Jack)\
**Post date:** [August 22, 2020, 10:01pm UTC](https://discourse.julialang.org/t/invoke-user-defined-package-functions-on-distributed-workers/45388/1 "2020-08-22T22:01:32Z")

</div>

Greetings! I created a package `ModelUtils` and hope to run it in multiple workers. In this package, a function called `linear_regression` being exported. The module is under `src` folder and has the following content

```julia
module ModelUtils
export linear_regression
include("linear_models.jl")
end

```

In `main.jl` file where I would invoke the `linear_models`

```julia
using Distributed
addprocs(exeflags="--project") # the --project flags is essentially running ] activate .
@everywhere using ModelUtils

fn_arg = rand(Float64, (5,2))

@sync for i in 1:nworkers()
    @spawnat i linear_regression(fn_arg)
end

```

I would get the following error

`UndefVarError: linear_regression not defined`

What is the cause for this error? Thank you.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 22, 2020, 10:21pm UTC](https://discourse.julialang.org/t/invoke-user-defined-package-functions-on-distributed-workers/45388/2 "2020-08-22T22:21:29Z")

</div>

Is that the full error message? if the function indeed is not defined on other worker processes, the error should be

```julia
ERROR: On worker 2:
UndefVarError: linear_regression not defined

```

If you don’t see the `On worker 2` check for typos in your function name and what you are exporting from the modula.

---

<div class="post-metadata">

**Author:** ![Jack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack/32/17406_2.png) [@Jack](https://discourse.julialang.org/u/Jack)\
**Post date:** [August 23, 2020, 12:26am UTC](https://discourse.julialang.org/t/invoke-user-defined-package-functions-on-distributed-workers/45388/3 "2020-08-23T00:26:26Z")

</div>

Thx for the answer. I checked for typos, and it’s not caused by typo.  
Adding `@everywhere` in the `linear_models.jl` and include using `include("src\\linear_models.jl")` in `main.jl` would work. I haven’t got `export` work smoothly. Some of the complications may involve linear\_regression has `pyimport` modules from python. I am good with `include` for now.
