# Running Julia from a Python script

**URL:** https://discourse.julialang.org/t/running-julia-from-a-python-script/92567
**Category:** General Usage
**Created:** [January 5, 2023, 7:09pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567 "2023-01-05T19:09:48Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 7:09pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/1 "2023-01-05T19:09:48Z")

</div>

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

```python
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.

---

<div class="post-metadata">

### Author: ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)
#### Post date: [January 5, 2023, 7:18pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/2 "2023-01-05T19:18:30Z")

</div>

Do you need the `-i` switch? Maybe leave that out? In the [manual](https://docs.julialang.org/en/v1/manual/command-line-options/#command-line-options) `-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.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 5, 2023, 7:21pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/3 "2023-01-05T19:21:05Z")

</div>

> [@erlebach](#):
>
> `os.system(cmd)`

I would suggest using the Python [`subprocess` module](https://docs.python.org/3/library/subprocess.html) 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](https://github.com/cjdoris/PythonCall.jl) 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.)

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 7:33pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/4 "2023-01-05T19:33:08Z")

</div>

> [@stevengj](#):
>
> something

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

---

<div class="post-metadata">

### Author: ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)
#### Post date: [January 5, 2023, 7:43pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/5 "2023-01-05T19:43:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 7:43pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/6 "2023-01-05T19:43:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)
#### Post date: [January 5, 2023, 7:43pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/7 "2023-01-05T19:43:47Z")

</div>

weird.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 7:45pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/8 "2023-01-05T19:45:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 7:58pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/9 "2023-01-05T19:58:05Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [January 5, 2023, 8:07pm UTC](https://discourse.julialang.org/t/running-julia-from-a-python-script/92567/10 "2023-01-05T20:07:59Z")

</div>

Even more weirdness. Under bash:

```julia
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:

```julia
 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'

```
