How include a file module in Julia 1.7?

Viewed 110

My goal is: in Julia 1.7 on Mac with M1 processor I would include a module file with many functions inside.

I've tried to follow this thread to generate my own package but it's generated an error.

I followed the answer but when i try importing MyPackage, Julia says: ArgumentError: Package MyPackage not found in current path.

With pwd() current path is Users/myname and in this folder MyPackage exists.

With command "import MyPackage" where i can see default folder for packages import ?

Where I get wrong ?

Sorry for my English.

1 Answers

I know two options here. Suppose I generated a package with the following file structure

MyPackage/
  src/
    MyPackage.jl
  Project.toml

and MyPackage.jl is

module MyPackage

export greet

greet() = print("Hello World!")

end # module

Then there are two options, I know

  1. Including of module MyPackage;
  2. Embedding of package MyPackage into Julia's ecosystem.

Suppose, that pwd is MyPackage/

First option is

julia> include("src/MyPackage.jl")
Main.MyPackage

julia> using .MyPackage

julia> greet()
Hello World!

Notice the dot in using statement.

Second option

Start Julia REPL and enter pkg mode, then

(@v1.6) pkg> dev MyPackage/
   Resolving package versions...
    Updating `~/.julia/environments/v1.6/Project.toml`
  [88e94d31] + MyPackage v0.1.0 `foo/bar/MyPackage`
    Updating `~/.julia/environments/v1.6/Manifest.toml`
  [88e94d31] + MyPackage v0.1.0 `foo/bar/MyPackage`

(@v1.6) pkg> st
      Status `~/.julia/environments/v1.6/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.1
  [0772a1fa] CubicEoS v0.2.0
  ..........
  [88e94d31] MyPackage v0.1.0 `foo/bar/MyPackage`
  ..........
  [09ab397b] StructArrays v0.6.5
  [a759f4b9] TimerOutputs v0.5.15

st command says that MyPackage became available in default environment.

Then you should be able to import/using the package. For the first time you should see precompiling message.

julia> using MyPackage
[ Info: Precompiling MyPackage [88e94d31-ecaf-41ca-ae10-053d89a189ff]

julia> greet()
Hello World!

P.S. The best place for local packages is .julia/dev/.

Related