Search code examples
juliatoml

How to run a Julia project?


Julia is not in my wheelhouse yet, but I have been handed a Julia project to run the code within. This consists of a directory containing a main.jl, a Project.toml and a Manifest.toml.

I've read up a little on what the TOML files are for; to summarise my current understanding, they form a project or environment (not sure which, or what the real difference is).

I have installed Julia v1.3.1 at the command line by downloading the tar, decompressing and placing in my path. Typing julia at the command line opens the Julia CLI REPL as expected.

I have tried to run the code by using julia main.jl, this results in complaints about the required packages not being present, e.g.:

julia main.jl
ERROR: LoadError: ArgumentError: Package JSON not found in current path:
- Run `import Pkg; Pkg.add("JSON")` to install the JSON package.

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:887
 [2] include at ./boot.jl:328 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1105
 [4] include(::Module, ::String) at ./Base.jl:31
 [5] exec_options(::Base.JLOptions) at ./client.jl:287
 [6] _start() at ./client.jl:460
in expression starting at /home/<user>/myproject/main.jl:3

I can follow the instructions here and load the required packages, but surely I shouldn't do this manually for every package? As every package required is listed in the Project.toml I guess there should be some way to tell Julia to make sure the packages in the project are made available (I'm thinking something along the lines of Python's requirements file).

I have tried julia --project=main.jl, but this just results in the REPL loading again with nothing happening (not sure if any project or environment is loaded or not).

How can I tell Julia to run the script in this project while taking note of the requirements and other information in the TOML files?

Update: Have figured out to enter ] at the REPL to enter the pkg package manager. Then I can:

(v1.3) pkg> activate .
Activating environment at `~/myproject/Project.toml`

(myproject) pkg> instantiate

(myproject) pkg>

Then leave the manager by pressing backspace. Still not sure how to "run" everything though.


Solution

  • You’re very close to the solution! If the files are all in a directory dir then the command would be

    julia --project=dir main.jl
    

    You could also start an interactive session in that environment and then run the code in the file via

    julia --project=dir
    
    julia> include(“main.jl”)
    

    Edit: If the directory is the current working directory, then you can just use --project=.