Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invoke Python code from Ruby?

Tags:

python

ruby

Does a easy to use Ruby to Python bridge exist? Or am I better off using system()?

like image 259
Eric Avatar asked May 07 '09 21:05

Eric


People also ask

How do you trigger a Python code?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

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.


2 Answers

You could try Masaki Fukushima's library for embedding python in ruby, although it doesn't appear to be maintained. YMMV

With this library, Ruby scripts can directly call arbitrary Python modules. Both extension modules and modules written in Python can be used.

The amusingly named Unholy from the ingenious Why the Lucky Stiff might also be of use:

Compile Ruby to Python bytecode.
And, in addition, translate that
bytecode back to Python source code using Decompyle (included.)

Requires Ruby 1.9 and Python 2.5.

like image 140
Bayard Randel Avatar answered Oct 06 '22 00:10

Bayard Randel


gem install rubypython

rubypython home page

like image 36
usr Avatar answered Oct 06 '22 00:10

usr