4

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 :-(.

Peter Mortensen
  • 759
  • 4
  • 7
berrynice
  • 163
  • 6
  • 3
    "Rapberry pi 3..." - is that some hip-hop version of the hardware? – ciao Apr 30 '16 at 23:33
  • 1
    Try evaluating SetEnvironment["LD_LIBRARY_PATH" -> ""] first. – ilian May 01 '16 at 01:43
  • It worked! OMG..
    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.
    Now, to win the stackexchange score, can you please answer why mathematica's LD_LIBRARY_PATH interferes with this and perhaps where I could read up more about the specifics?
    – berrynice May 01 '16 at 06:21
  • ..that was a typo y'all! RaSpberry. – berrynice May 01 '16 at 06:28
  • ..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)
    – berrynice May 01 '16 at 06:47
  • It's a conflict between system libraries (most likely openssl) required by the python code and the ones included in the Mathematica layout needed for functions like URLFetch or Encrypt -- there should be no adverse consequences unless the script depends on these. See also this answer. – ilian May 02 '16 at 02:09
  • @illian that makes sense. plus, your advice on the other post re:restoring original values by (below) is important. if you repost your comment as an 'Answer' I'll accept it. thanks.

    SetEnvironment["LD_LIBRARY_PATH" -> FileNameJoin[{$InstallationDirectory, "SystemFiles", "Libraries", $SystemID}]]

    – berrynice May 02 '16 at 10:06

0 Answers0