Calling Python from Ruby

Calling Python from Ruby

This article gives some techniques for running Ruby code from Python which should also be applicable in the reverse direction (such as XML-RPC or pipes) as well as specific techniques for running Python code from Ruby. In particular rubypython or Ruby/Python look like they may do what you want.

Call python script from ruby

One way would be exec.

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

How to Run a Python Script from a Rails Application?

Here are ways to execute a shell script

`python pythonscript.py`

or

system( "python pythonscript.py" )

or

exec(" python pythonscript.py")

exec replaces the current process by running the given external command.

Returns none, the current process is replaced and never continues.

Run a Ruby Script with Python

Most probably, ruby in your python script is not the same as ruby in your shell. This can happen if you e.g. have a default system ruby installed (e.g. from the system packages) and you install another ruby version via RVM.

When using shell, the RVM automatically loads it's environment from the shell init scripts (e.g. ~/.bashrc) and knows which ruby to use from its settings. Whereas your python script does not load any rvm environment (it's not a login shell) and calls the default system ruby.

In that case, you need to explicitly call the correct ruby from the RVM in your python script. You can to it by calling the RVM wrapper:

  • browse directories under ~/.rvm/wrappers/ and find the correct ruby version and gemset that you want to use
  • in your python script, call the ruby command from this wrapper directory instead of the plain `ruby, something like:
rvm_ruby = os.environ['HOME'] + "/.rvm/wrappers/ruby-2.3.0-p100@myproject/ruby"
os.system(rvm_ruby + " ./wpscan.rb -u www.mysite.com")

This should fix your problem.

Ruby: get return value of python script

First, have your Python script print the value rather than returning it, or add print main() at the bottom so the return value of main() gets printed.

Second, on the Ruby side, execute it with backticks rather than the system() function, like this:

output = `python script.py`

This captures the output of the Python script as a string. If you want it as a Ruby array, you'll need to parse it. Ruby's array literal syntax is similar to Python's list literal syntax, so this is not as tough as it might seem. If you can find something that parses strings into Ruby arrays (besides eval() because it's dangerous) you should be covered. Problems will arise when you have things besides simple types, None, or potentially strings with escapes in them.

I am more a Python guy than a Ruby guy, but Ruby doesn't seem to have anything like Python's ast.literal_eval (a safe eval() that only accepts literals) in its standard library. However, I did find parsr which appears to be exactly that.

If the Python list literals you're getting aren't valid Ruby, you can use JSON as the interchange format:

# Python side
import json, sys
json.dump(main(), sys.stdout)

# Ruby side
require 'json'
output = JSON.parse(`python script.py`)

How can I run Python code in Ruby?

This is a completely overdone, over-complicated way to do this, but here's how anyway.

Say your python module (file) is named googstock.py. It should look like this:

import ystockquote

def run():
return ystockquote.get_all('GOOG')

Then your Ruby can look like this:

RubyPython.start

googstock = RubyPython.import('googstock')
puts googstock.run().rubify #Get the stock data into Ruby

RubyPython.stop

In the future though, try just getting the stock values from Yahoo!'s API in Ruby. It can't be that hard.

Call python code from Ruby

RubyPython allows you to use Python objects from within Ruby, but your code has to be valid Ruby code (it just exposes wrapper objects that work with the Python objects in the background). You could translate your code into the Ruby equivalent using this tool, but at that point, you might as well just rewrite it in Ruby, which would be cleaner, more readable, and more efficient.

There is no way, as far as I know, to insert Python code into a Ruby script and have it run. Such a project is pretty out-there and unlikely to be anything approaching efficient.

Either rewrite your Python code as Ruby code, or make your Python code a stand-alone script that you can then call the Python interpreter to run it from Ruby. This is naturally only an option if your code doesn't need a ton of interaction with the rest of your application, as all communication will have to happen using stdin/stdout.

Run python from a click event in ruby

Check this SO post: Call python script from ruby

ruby provides a variety of methods, exec, system that all take the path to the ,py file along with arguments. Something like

result = exec("python /path/to/script/script.py params")

Or

system 'python /path/to/script/script.py', params1, params2

Or even backquotes

`python /path/to/script/script.py foo bar`

Now, the question is where to put this? I am assuming you have a controller that handles clicks, and it is this controller where you put this code. Check this link out How to integrate a standalone Python script into a Rails application?

Now details depend a lot on your python script. By 'alert script', do you mean a Javascript alert() dialog, a flash msg or anything else? It's always a good idea to refactor your python code to return a string result and the use that in ruby-on-rails. Even better, as far as possible, write the alert-creating-script in ruby itself! It might not be possible in a 3rd-party library setting, but for a simple stand-alone script, it's worth the effort!



Related Topics



Leave a reply



Submit