I want to import two libraries, normally in Python the line of code are:

import json
import requests

How I import these in Python service? 

kwatters

8 years 8 months ago

Hi Papa,

  MRL does have a scripting interface for the python scripting lanaguage.  That scripting interface is based on a java implementation of python called "jython".  Jython does not have as many of the build in native libraries as python.  However you can either import java objects into the jython interpreter, or you can use pure python based implementations of it.

  I suppose the question is, what are you trying to do? there might be an easier way.

best,

  -kwatters

GroG

8 years 8 months ago

Kwatters is correct of course - it always helps if you can tell us your "goal" 

But here is some hopefully helpful information:

1. You should always at least say what version your working with.  It is important - MRL is always changing (this is good - its getting better) - but if we don't know what version your using, it may be completely different from the version we are testing.  Its good to be on the "same page" - always consider sending a noWorky - it always has the version in it.

2. Your probably using Jython 2.5.2,  I just pushed Jython 2.7.0 into the "develop" repo and updated the serviceData.json - "json" comes with Jython 2.7.0 so you can import that library theoretically if create a new directory - download the latest jar - and install all the services ... Jython 2.7.0 should come down when re-installing the services.

3. It turns out you can install extended libraries if you make a Lib directory in the same directory the Jython jar is in.  In your case this should be (mrl install directory)/develop/libraries/jar/Lib  You can dump Python modules in this directory, restart MRL and you'll be able to import them.  The only caveat is they have to be "pure Python" - no C code allowed ;) 

4. You can print out where your Jython jar is and where your Lib directory should be, by doing :
print (sys.path)

See below I am using Jython 2.7.0 and its being supplied by a Jython jar in the repo.  If I make a Lib directory there, dump some python files in and restart - I will have access to those modules

I want use Microsoft Translator in MyRobotLab, could you help me? I've this code in Python:

import json
import requests
import urllib
args = {
        'client_id': '',#your client id here
        'client_secret': '',#your azure secret here
        'scope': 'http://api.microsofttranslator.com',
        'grant_type': 'client_credentials'
    }
oauth_url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
oauth_junk = json.loads(requests.post(oauth_url,data=urllib.urlencode(args)).content)
translation_args = {
        'text': "hello",
        'to': 'fr',
        'from': 'en'
        }
headers={'Authorization': 'Bearer '+oauth_junk['access_token']}
translation_url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
translation_result = requests.get(translation_url+urllib.urlencode(translation_args),headers=headers)
print translation_result.content