Calling a julia script from within another script

I have a julia file that contains functions and code that uses those functions. I would like to create a separate julia script that is able to execute this script (and obtain one of the values that is created at the end of the script).

However, I would like the separate julia script to be able to pass a parameter to the first script when calling it. I can’t figure out how to do this part. I’m a beginner to julia, so I’d appreciate an answer in simple terms!

You could just include the script? And expose functions?

1 Like

Is a command-line parameter that you are talking about? Julia has ARGS that is a global vector with the command-line parameters as Strings. You can change the first script to check the arguments there, and then call it from the second script as you can do with any script (with run).

But why would you want to do that and not include it in the first place?

1 Like

In Julia, calling the script from another script is not at all a common practice.

Here is how you can achieve the desired behavior:

Content of script to be called (let’s name it script-a.jl):

function fwithparam(x)
    x + 1

Caller content (let’s say script-b.jl):

include("script-a.jl") # assuming it is in the same directory

# now - you have access to your fwithparam function
println(fwithparam(10)) # should print 11

Because the interface may only be defined at script level? If you include and in the future you change the name of the functions, or how you do the things, or the language of the script then the including script will break. Other reason is that if you start using include in a frequent manner, you will start having problems with script C includes script B and A, and script B includes script A too, but then you have double included A and things may start to get in disarray.

Here is how you can do it if you (for any reason) prefer this approach.

Let’s put this in the script to be called from another script (let’s say A.jl):

x = ARGS[1]
callx(x) = parse(Int, x) + 1

Now, in the script that will call/execute A.jl, we can add (let’s call this B.jl):

myx = 5
output = readchomp(`julia A.jl $myx`)
println("I received: $output from script execution of A.jl...")

Now, if I go in terminal and run:

julia B.jl

… you’ll see the script B.jl calling A.jl and you’ll get the following output:

I received: 6 from script execution of A.jl...

In this way, you don’t need to care about what the functions are called inside A.jl - you just need to make sure you are printing stuff to stdout.

Have fun.

1 Like

Use Home · Comonicon.jl for adding cli to the first script. See Running External Programs · The Julia Language to generate Cmd that includes the arguments in the second script.

It’s not clear what your goals and motivations are. I suppose you could put the functions into a single file, perhaps organized into a module, and then include this from other .jl files, as ufechner7 wrote.

@clairem, I see that you picked one of my posts as the solution - however, I think you wanted to pick the other one where I answered specifically to your original question.

I am only writing this to make it easier for other users with similar questions that might stumble upon this thread.

1 Like