4
$\begingroup$

It's a steep learning curve trying to integrate Mathematica with Python's modules/library. I am using a Raspberry Pi 3 with Mathematica's Wolfram command line. I know little about both platforms (so clearly Wolfram's genius/strategy is working on me! - LoL...)

My objective: Run .py scripts from within Mathematica's scripts (.m):

Problem: It works fine from within the shell command line, but when using the mathematica - script option, I get this error:

File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 930, in <module>
    class HTTPSConnectionWithTimeout(httplib.HTTPSConnection):
AttributeError: 'module' object has no attribute 'HTTPSConnection'

For some reason, Python invoked via Mathematica seems to load modules differently.

For example:

python -v output shows me that from the shell the SSL module:

import ssl # precompiled from /usr/lib/python2.7/ssl.pyc

Whereas from Mathematica, there is an additional line:

import ssl # precompiled from /usr/lib/python2.7/ssl.pyc
dlopen("/usr/lib/python2.7/lib-dynload/_ssl.arm-linux-gnueabihf.so", 2);

My scripts are simple: The Mathematica .m file is just:

 SetEnvironment[
 "PYTHONPATH" ->
  "/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python3.4/\
dist-packages"]; txtResult =
 Import["!/usr/bin/python /home/pi/aprilbot.py", "Text"]

And aprilbot.py imports module pytumblr, a Tumblr API client and posts a text message:

#!/usr/bin/python

import pytumblr

consumer_key='xyz'
consumer_secret='xyz'
token_key = 'xyz'
token_secret='xyz'

client = pytumblr.TumblrRestClient(
 consumer_key,
 consumer_secret,
 token_key,
 token_secret
)

blogname = 'ABC'

client.create_text(blogname, state="published", slug="testing-post", title="Testing", body = "my text post..")

print('done')

I am stuck :-(.

$\endgroup$
7
  • 3
    $\begingroup$ "Rapberry pi 3..." - is that some hip-hop version of the hardware? $\endgroup$
    – ciao
    Commented Apr 30, 2016 at 23:33
  • 1
    $\begingroup$ Try evaluating SetEnvironment["LD_LIBRARY_PATH" -> ""] first. $\endgroup$
    – ilian
    Commented May 1, 2016 at 1:43
  • $\begingroup$ <b><i> It worked!<i><b> OMG..<br> A quick google reveals that its because LD_LIBRARY_PATH is given priority over /lib and /usr/lib. fab. you've totally won my appreciation. thank you @ilian. <br>Now, to win the stackexchange score, can you please answer <i>_why_<i> mathematica's LD_LIBRARY_PATH interferes with this and perhaps where I could read up more about the specifics? $\endgroup$
    – berrynice
    Commented May 1, 2016 at 6:21
  • $\begingroup$ ..that was a typo y'all! RaSpberry. $\endgroup$
    – berrynice
    Commented May 1, 2016 at 6:28
  • $\begingroup$ ..and..I am wondering.. what's the consequence/side effect to the wolfram system? Having inspected LD_LIBRARY_PATH in a new session shows: /opt/Wolfram/WolframEngine/10.3/SystemFiles/Libraries/Linux-ARM:/opt/\ Wolfram/WolframEngine/10.3/SystemFiles/Libraries/Linux-ARM/Mesa (I'm thinking best-practice etc etc) $\endgroup$
    – berrynice
    Commented May 1, 2016 at 6:47

0

Browse other questions tagged or ask your own question.