Volt
Volt

Reputation: 127

Calling python shell apps from ruby (rails)

How can I call python running in shell from ruby. Actually I need ruby to communicate with some app running under the python in shell. So I need ruby to call python (command 'python') in cmd and then import libraries (command 'import xyz') under the python environment, and thne run functions of this imported libraries (for example 'xyz.showdate()'), and get result of course.

I tryed to use IO.popen and Open3.popen3 functions to achieve this, but I do something wrong.

Actually I use jruby on rails, but it should be same with ruby, I guess.

Upvotes: 0

Views: 593

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55197

You probably don't really want to be doing this, this is going to make your ruby script excessively convoluted when you could probably get the result directly in ruby.

If you however decide to go that way, you could create a python script that does what you need and run it from the ruby script:

# python-script.py 
import xyz
if __name__ == '__main__':
    xyz.showdate()

You would then arrange so that your ruby script calls the following command:

python path/to/python-script.py

Provided your xyz.showdate prints the date, you can collect standard output to get the result.

But again, that's probably not a great idea.

Upvotes: 2

Related Questions