How to execute external scripts of python/bash/ruby from elixir?

Hi all,
I’m facing with a problem that I didn’t find while searching in Google.
I need to execute python scripts from elixir, so I tried System.cmd/3 and it didn’t work.
Any way to do so on elixir?
E.g. I have file on /tmp/1.py and I want to run it from my Elixir.

Thanks for your help!
SL

What exactly have you tried when using System.cmd/3? And what error did you get?

Possible solutions, based on the most common mistakes made with System.cmd/3:

  • If your script is executable and in path System.cmd "yourscript.py", [] should just work.
  • If your script is not in path but executable you’ll have to prive absolute path: System.cmd "/tmp/yourscript.py", []
  • If your script is neither, you need to specify an interpreter: System.cmd "python", ["/tmp/yourscript.py"]
3 Likes

Hey!
Thanks for your reply!
I finally success to run it with the following:

System.cmd “python” [“myscript.py”]
Now i’ll have to figure how to put response.

Thanks again!

What do you mean by “put response”?

Do you mean this:

iex(1)> {out, _} = System.cmd "python", ["-c", ~s(print "Hello!")]
{"Hello!\n", 0}
iex(2)> IO.puts out
Hello!

:ok

put == get.
Yea that’s what I did too.
Thanks again!

You may what to have a look at The Erlangist - Outside Elixir.

1 Like