eterey
eterey

Reputation: 420

Call python script from ruby

For example, I can use Python scripts in PHP like there:

exec("python script.py params",$result);

where "script.py" - script name and variable $result save output data.

How I can make it with Ruby? I mean, call Python scripts from Ruby.

Upvotes: 19

Views: 23700

Answers (5)

Prashanth Sams
Prashanth Sams

Reputation: 21129

You can use backticks to accomplish this in two ways

result = `python script.py params`

or

result = %(python script.py params)

Upvotes: 0

MatthewSteen
MatthewSteen

Reputation: 101

I used the following python call from a ruby module.

In my case using exec() didn't work because it interrupts the current process and caused the simulation I was running to fail.

# run python script
`python #{py_path_py} #{html_path_py} #{write_path_py}`

Upvotes: 1

Sandeep
Sandeep

Reputation: 21144

Another way to do the same thing would be,

system 'python script.py', params1, params2

Upvotes: 3

steenslag
steenslag

Reputation: 80065

One way would be exec.

result = exec("python script.py params")

Upvotes: 11

maniacalrobot
maniacalrobot

Reputation: 2463

You can shell-out to any shell binary and capture the response with backticks:

result = `python script.py params`

Upvotes: 30

Related Questions