How to run a Julia project?
Asked Answered
R

2

12

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.

Rhea answered 16/3, 2020 at 17:29 Comment(0)
P
10

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=.

Parley answered 16/3, 2020 at 17:47 Comment(2)
Turns out the script wasn't set up to run immediately. After includeing as in this answer, I needed to call the function I wanted to run, e.g. julia> main("some string", SomeConstant).Rhea
$ julia --project=. ./src/main.jl ERROR: LoadError: ArgumentError: Package Pluto [c3e4b0f8-55cb-11ea-2926-15256bba5781] is required but does not seem to be installed: - Run 'Pkg.instantiate()' to install all recorded dependencies.Tanah
S
0

The error message Package JSON not found in current path imply that you don't have JSON installed.

You can check this by starting Julia and type using JSON

To install JSON all you have to do is writing import Pkg; Pkg.add("JSON")

See this output for example:

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.5.2 (2020-09-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using JSON
ERROR: 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:893

julia> import Pkg; Pkg.add("JSON")
   Updating registry at `~/.julia/registries/General`
   Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Resolving package versions...
Updating `~/.julia/environments/v1.5/Project.toml`
  [682c06a0] + JSON v0.21.1
Updating `~/.julia/environments/v1.5/Manifest.toml`
  [682c06a0] + JSON v0.21.1
  [69de0a69] + Parsers v1.1.0
  [ade2ca70] + Dates
  [a63ad114] + Mmap
  [de0858da] + Printf
  [4ec0a83e] + Unicode

julia> using JSON
[ Info: Precompiling JSON [682c06a0-de6a-54ab-a142-c8b1cf79cde6]

julia> 
Skipjack answered 24/5, 2021 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.