# How to invoke library from function

**URL:** <https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 29, 2020, 9:04pm UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591 "2020-12-29T21:04:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 29, 2020, 9:04pm UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/1 "2020-12-29T21:04:14Z")

</div>

I am setting a function (let’s say, `A`)that requires a library. I would like to put the function in a common file and call it with `include` from other files. To use such a function as any other Julia functions, the library should be updated only when the A is called, but if I put `using` inside `A`, I get the error

```julia
syntax: "using" expression not at top level

```

What would be the correct syntax to call a library inside a function?  
Thank you

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 30, 2020, 12:14am UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/2 "2020-12-30T00:14:18Z")

</div>

Well, you don’t call the library, you call a function from the library. Thus, put the `using` outside the function. If you want to “hide” the library from the global scope, put the `using` and the function inside a module and export the function:

```julia
module ContainsA
  import Library
  A(x) = 2*Library.f(x)
  export A
end

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 30, 2020, 6:56am UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/3 "2020-12-30T06:56:30Z")

</div>

Thank you, that is interesting. I tried with this:

```julia
module plotter
  using PyPlot
  function plotIt(y)
    clf()
    myPlot = plot(1:length(y), y, color = "blue", linestyle = "-")
    ax = gca()
    xlabel("X-axis")
    ylabel("Y-axis")
    Title = string("Vector of lenght ", length(y))
    title(Title)
    savefig("pyPlotted.png",
        dpi = 300, format = "png", transparent = false)
  end
end

```

I saved it in the file ~/plotter.jl. The function on its own works but when I call the file on a vector I prepared:

```julia
julia> upwards
25-element Array{Any,1}:
  50000
  79914.30104963228
 127644.309471282
 203673.98788725163
 324462.31598247594
 515551.7595856664
 815844.3952348703
      1.28279201312967e6
      1.9969756490546213e6
      3.0617026996446643e6
      4.588540941518327e6
      6.6561029840302905e6
      9.238094228093313e6
      1.213357406412708e7
      1.4980645412844565e7
      1.7404312164067835e7
      1.9199775856128015e7
      2.0383414901214056e7
      2.109967655786273e7
      2.1509439686879102e7
      2.173604725412978e7
      2.1858962851902638e7
      2.1924924681004412e7
      2.1960117884318497e7
      2.197883647128726e7
julia> include("~/plotter.jl")
Main.plotter
julia> plotIt(upwards)
ERROR: UndefVarError: plotIt not defined
Stacktrace:
 [1] top-level scope at none:1

```

What am I missing?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 30, 2020, 7:19am UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/4 "2020-12-30T07:19:24Z")

</div>

you did not export the function…

```julia
Main.plotter.plotIt

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 30, 2020, 12:17pm UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/5 "2020-12-30T12:17:28Z")

</div>

Actually you need to export the function and use the module with:

```julia
include("MyModule.jl")
using .MyModule #note the dot .

```

so that the exported functions are available without using explicitly `MyModule.f`.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 30, 2020, 4:32pm UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/6 "2020-12-30T16:32:42Z")

</div>

> [@jling](#):
>
> `Main.plotter.plotIt`

where does it go? into ~/plotter.jl? or in the calling script? Does it need a statement?

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [December 30, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/7 "2020-12-30T18:39:43Z")

</div>

You want to call `plotIt` from outside the module `plotter`. That is why jling said that `plotIt` is at `Main.plotter.plotIt`. So, basically, you’ve loaded the module `plotter` into the Main REPL, and `plotIt` is inside that.

To call it, use

```julia
julia> include("~/plotter.jl")
Main.plotter

julia> using .plotter # Load local module.

julia> plotter.plotIt(upwards)

```

Or, if you add `export plotIt` below the line `module plotter`, then you can use

```julia
...

julia> using .plotter

julia> plotIt(upwards)

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 31, 2020, 6:55am UTC](https://discourse.julialang.org/t/how-to-invoke-library-from-function/52591/8 "2020-12-31T06:55:58Z")

</div>

OK, now I got it. Thank you all.
