How to invoke library from function

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

syntax: "using" expression not at top level

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

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:

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

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

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> 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?

you did not export the function…

Main.plotter.plotIt
2 Likes

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

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

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

1 Like

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

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> 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> using .plotter

julia> plotIt(upwards)
1 Like

OK, now I got it. Thank you all.

2 Likes