Running Julia from a Python script

I would like to run Julia from a Python script, such as:

import os

for o in [1,2]:
        cmd = f'julia -i --project="." -- tst.jl --omega={o}'
        os.system(cmd)

However, when I execute the script, I get subshell windows popping up on my screen. I want to avoid this from happening. When I execute a Python script from this script, the command executes in a subshell, but not in a new window. I would like the same behavior for Julia so that I can see the Julia output of all runs in a single window.

Do you need the -i switch? Maybe leave that out? In the manual -i turns on interactive mode, but if you don’t want windows popping up you probably don’t want interactive mode.

edit: I just tried a simple, os.system("julia test.jl") on my machine and it didn’t open any windows.

1 Like

I would suggest using the Python subprocess module instead of os.system so that you don’t launch a shell at all, and instead just spawn the julia process directly.

(Generally, speaking, it would be even better to avoid spawning processes entirely. Instead, use something like JuliaCall to call Julia as a library / module. This lets you communicate directly by passing numbers/arrays/etcetera back and and forth rather than communicating via strings.)

3 Likes

Thank you, @mihalybaci . I originally did not have the -i switch, and yet the window opened. I am using the zsh shell on the mac, so that could be part of it.

@stevengj , thanks. I will try your suggestions. In the meantime, I was learning to use the Julia call run.

You might need to drop the leading dashes in front of the omega variable either. Not sure if that’s causing the issue. FWIW, I’m using Python from VS Code on Windows, and I tried more keywords to be more similar to your example, but os.system("""julia --project="." -- test.jl a=10""") still doesn’t have any popups.

I checked with bash: no window opens. So it was a choice of shell issue. Thanks!

1 Like

weird.

Yep, weird. Perhaps there is a zsh option to alleviate this issue. Don’t know. I would rather not switch to bash, but it is an option.

Here is more weirdness. When running my python script under zsh, I get a window popping up, but the code works. Then I start a bash subshell, run the python script and get the error:

executing .bashrc

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ python run_cases.py
julia --project="." -- argmacros_example.jl --omega=0.01 --gam0=0.01
inside startup.jl
ERROR: LoadError: ArgumentError: Package ArgMacros [dbc42088-9de8-42a0-8ec8-2cd114e1ea3e] is required but does not seem to be installed:
 - Run `Pkg.instantiate()` to install all recorded dependencies.

Stacktrace:
 [1] _require(::Base.PkgId) at ./loading.jl:999
 [2] require(::Base.PkgId) at ./loading.jl:928
 [3] require(::Module, ::Symbol) at ./loading.jl:923
 [4] include(::Function, ::Module, ::String) at ./Base.jl:380
 [5] include(::Module, ::String) at ./Base.jl:368
 [6] exec_options(::Base.JLOptions) at ./client.jl:296
 [7] _start() at ./client.jl:506
in expression starting at /Users/erlebach/src/2022/rude/giesekus/argmacros_example.jl:1

I do not understand how that is possible. Thanks.

Even more weirdness. Under bash:

import shlex
import subprocess

omega = [.01]
gam0 = [.01]

for o in omega:
    for g in gam0:
        # Make sure the Julia writes everything to files
        cmd = f'julia --project="." -- argmacros_example.jl --omega={o} --gam0={g}'
        cmd = 'julia'
        print(cmd)
        args = shlex.split(cmd)
        print(args)
        p = subprocess.Popen(args)

I get the error:

 python run_cases.py
julia
['julia']
Traceback (most recent call last):
  File "run_cases.py", line 21, in <module>
    p = subprocess.Popen(args)
  File "/Users/erlebach/opt/anaconda3/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Users/erlebach/opt/anaconda3/lib/python3.8/subprocess.py", line 1706, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: 'julia'