0

I am trying to run a python script from ruby method. I am running this method as a rake task within a Rails app. I am using the solution mentioned here:

def create
    path = File.expand_path('../../../../GetOrders', __FILE__)
    output = `"python2 " + path + "/parse.py"`
    print output
    str = JSON.parse(output)
    print str
end

EDIT: This works:

    output = `python2 #{path}/parse.py`

EDIT2: Using the python script i am trying to pass a list of dictionaries to the ruby function. The python script looks something like:

import xml.etree.ElementTree as ET
import json

def parse():
    tree = ET.parse('response.xml')
    root = tree.getroot()
    namespaces = {'resp': 'urn:ebay:apis:eBLBaseComponents'}
    order_array = root.find("resp:OrderArray", namespaces=namespaces)
    detailsList = []
    for condition:
        details["key1"] = value1
        details["key2"] = value2
    detailsList.append(details)
    output = json.dumps(detailsList)
    return output

print parse()

Could someone explain what i am doing wrong and how can I fix this. Thanks

14
  • can you run it directly on the command line? Commented Feb 8, 2014 at 18:40
  • @thorstenmüller : yes, the python script executes fine from command line Commented Feb 8, 2014 at 18:40
  • 2
    the output of the script may not be sent to stdout? could that be possible? Commented Feb 8, 2014 at 18:49
  • 1
    no, something like print parse() in the last line of parse.py Commented Feb 8, 2014 at 19:08
  • 2
    OK, so now you have narrowed this problem down to an issue in your python program. It is no longer about Ruby. I think you should ask a new question on this site that is just about the Python. Show the full program, the actual output, and the expected output. You are probably just using the Python JSON libary incorrectly. If Ruby is going to be parsing the JSON, then Python should output JSON directly instead of trying to parse it at all, which it seems like dumps might be doing. There is no reason to be parsing JSON on the Python and Ruby side as far as I know. Commented Feb 8, 2014 at 22:36

3 Answers 3

1

When you do this:

output = `python2 #{path}/parse.py`

output will be assigned the standard output of the python script, but that script isn't writing anything to standard output; the json data that's the return value of the parse() call is simply discarded. You seem to be expecting the execution of the script to have a "return value" that's the return value of the script's last expression, but that's not how processes work.

You probably want to replace the parse() call at the end of the script with print parse().

Sign up to request clarification or add additional context in comments.

1 Comment

Actually i made a mistake. This gives a weird error. Updated the question.
1

You are calling this exact line on the shell:

"python2 -path- /parse.py"

which the shell interprets as a single command: python2 (with a space at the end).

Try using string interpolation, which works with the backtick operator:

output = `python2 #{path}/parse.py`

3 Comments

I already tried this: output = python2 #{path}/parse.py. It does not print anything. mentioned in the edits of the question
@nish Are you sure your Python script does print something on the standard output?
edited. Sry for posting wrong one earlier. The python script works fine.
1

Imagine typing this exact string:

"python2 " + path + "/parse.py"

into your shell (e.g. bash). It would look for a program named "python2 " and give it four arguments

+
path
+
/parse.y

You can't put arbitrary Ruby code inside a backtick string the same way you can't put arbitrary code in normals strings. You must use string interpolation.

3 Comments

I already tried string interpolation, it does not give any error. But does not print anythin either. mentioned in the EDIT in th question
Then you should post all your code after simplifying it: SSCCE.org.
%x{..} wouldn't accept string, so "python2 " + path + "/parse.py", will trow error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.