How to run a julia script through terminal
Asked Answered
R

3

11

I have a script main.jl which prints a simple "Hello world" string:

println("Hello world!")

However, when trying to run the script through the terminal like this:

julia> main.jl

I get the error:

ERROR: type #main has no field jl

All the information I can find online suggests calling the script like I do to run it. I have assured that I'm in the correct directory - what am I doing wrong?

Righthander answered 17/2, 2022 at 14:48 Comment(2)
Try julia> include("main.jl")Whitewood
I'm curious about the error message though. Both in a normal REPL and within VS Code REPL, I get a different error message (main not defined), so I'm curious why you're getting a message about type #main. Could you mention where you're running this i.e. is this VS Code, or Juno, or elsewhere? And which OS?Atmospheric
A
11

You are trying to run the file from the Julia REPL (as indicated by the julia> prompt at the beginning of the line). There, you have to include the file as @AndreWildberg mentions. This will run the commands from the file as if you had typed them into the REPL.

The information you found online might have been about running Julia from "normal" terminal, aka a console shell like bash on Linux. There, running julia main.jl will run the program, although the REPL method above is usually preferred for working with Julia.

(question about calling the script with arguments asked in the comment):
First of all, I'll mention that this is not the usual workflow with Julia scripts. I've been writing Julia code for years, and I had to look up how to handle command-line arguments because I've never once used them in Julia: usually what's done instead is that you define the functions you want in the file, with maybe a main function, and after doing an include, you call the main function (or whichever function you want to try out) with arguments.

Now, if your script already uses command-line arguments (and you don't want to change that), what you can do is assign to the variable that holds them, ARGS, before the include statement:


julia> push!(empty!(ARGS), "arg1")
1-element Vector{String}:
 "arg1"

julia> include("main.jl")

Here we empty the ARGS to make sure any previous values are gone, then push the argument (or arguments) we want into it. You can try this out for educational purposes, but if you are new to the language, I would suggest learning and getting used to the more Julian workflow involving function calls that I've mentioned above.

Atmospheric answered 17/2, 2022 at 15:15 Comment(2)
Ah I see. I have been trying to run it through a julia terminal in VS Code on a Windows 10 machine. What if I wanted to call my script with arguments? By trial, I can observe that it is not like this: include("main.jl") <arg1>Righthander
Actually, for the usage as "script" I believe running the script from terminal is better than including the code. The problem is that if you "call" the script by including it multiple times you may have nasty problems due to multiple redefinitions, while if you call the script from terminal you are always in a clean situation.Monied
A
3

The julia> prompt means your terminal is in Julia REPL mode and is expecting valid Julia code as input. The Julia code main.jl would mean that you want to return the value of a field named jl inside a variable named main. That is why you get that error. The Julia code you would use to run the contents of a file is include("main.jl"). Here the function include is passed the name of your file as a String. This is how you should run files from the REPL.

The instructions you read online are assuming your terminal is in shell mode. This is usually indicated by a $ prompt. Here your terminal is expecting code in whatever language your shell is using e.g. Bash, PowerShell, Zsh. When Julia is installed, it will (usually) add a julia command which works in any shell. The julia command by itself will transform your terminal from shell mode to REPL mode. This julia command can also take additional arguments like filenames. If you use the command julia main.jl in this environment, it will run the file using the Julia program and then exit you back to your terminal shell. This is how you should run files from the terminal shell.

A third way to run Julia files would be to install an IDE like VSCode. Then you can run code from a file with keyboard shortcuts rather than by typing commands.

See the Getting Started Documentation for more information.

Aker answered 17/2, 2022 at 16:15 Comment(0)
H
0

Adding to Sundar R's answer, if you want to run script which takes commandline arguments from REPL, you can check this package: https://github.com/v-i-s-h/Runner.jl

It allows you to run you script from REPL with args like:

julia> @runit "main.jl arg1 arg2"

See the README.md for detailed examples.

Hadsall answered 17/1, 2023 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.