Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call python script from ruby

Tags:

python

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.

like image 820
uzumaxy Avatar asked Sep 05 '13 20:09

uzumaxy


People also ask

Can you call Python from Ruby?

PyCall: Calling Python functions from the Ruby language. This library provides the features to directly call and partially interoperate with Python from the Ruby language. You can import arbitrary Python modules into Ruby modules, call Python functions with automatic type conversion from Ruby to Python.

Can you use Python and Ruby together?

Using Ruby with Python - a powerful combination If you want to build a web app that looks great, delights users, and handles Big Data or uses Machine Learning, joining the forces of Python and Ruby is a very good idea.

How do I run a Python script in rails?

To do so, you would need to make your script into a proper Python module, start the bridge when the Rails app starts, then use RubyPython. import to get a reference to your module into your Ruby code. The examples on the gem's GitHub page are quite simple, and should be sufficient for your purpose.


4 Answers

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

result = `python script.py params`
like image 80
maniacalrobot Avatar answered Oct 04 '22 11:10

maniacalrobot


One way would be exec.

result = exec("python script.py params")
like image 35
steenslag Avatar answered Oct 04 '22 11:10

steenslag


Another way to do the same thing would be,

system 'python script.py', params1, params2
like image 37
Sandeep Avatar answered Oct 04 '22 12:10

Sandeep


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}`
like image 1
MatthewSteen Avatar answered Oct 04 '22 11:10

MatthewSteen