How can I use python requests or other 3rd party packages in general?

Hi everybody,

I am using Windows 8.1 and LibreOffice 4.4.2.2.
Scripting with the standard library of Python seems to work quite well, but I am having a hard time making the package “requests” work.
Since I couldn’t find pip or anything like that I downloaded the source directory into
C:\Program Files (x86)\LibreOffice 4\program
and executed within the source directory
“C:\Program Files (x86)\LibreOffice 4\program\python.exe setup.py install”,
which seemed to work at the first glance, but when I try to run the following script:


import ctypes, requests

def blabla(*args):
   
    yahoo_url="http://download.finance.yahoo.com/d/quotes.csv?s=" + "+ABX+ALV.DE" +"&f=l1t1d1xphgkjva2m3m4on&e=.csv"
    r = requests.get(yahoo_url)
    ctypes.windll.user32.MessageBoxW(0, r.text, "Yahoo says:", 1)`

I get the following error message:

Ein Scripting Framework Fehler trat während der Ausführung vom Python-Skript vnd.sun.star.script:PyOffice|coco.py$blabla?language=Python&location=share auf.

Meldung: : cannot import name utils
  C:\Program Files (x86)\LibreOffice 4\program\python-core-3.3.3\lib\site-packages\requests\__init__.py:58 in function () [from . import utils]
  C:\Program Files (x86)\LibreOffice 4\program\uno.py:265 in function _uno_import() [return _g_delegatee( name, *optargs, **kwargs )]
  C:\Program Files (x86)\LibreOffice 4\share\Scripts\python\PyOffice\coco.py:1 in function () [import ctypes, requests]
  C:\Program Files (x86)\LibreOffice 4\program\pythonscript.py:451 in function getModuleByUrl() [exec(code, entry.module.__dict__)]
  C:\Program Files (x86)\LibreOffice 4\program\pythonscript.py:992 in function getScript() [mod = self.provCtx.getModuleByUrl( fileUri )]

Thx in advance for any help!

Hallo

Cannot reproduce the issue, but I’m on Linux, most of this stuff is much more easier which real OS.

try to edit C:\Program Files (x86)\LibreOffice 4\program\python-core-3.3.3\lib\site-packages\requests\__init__.py

change Lines 58 to 63 to:

from requests import utils
from requests.models import Request, Response, PreparedRequest
from requests.api import request, get, head, post, patch, put, delete, options
from requests.sessions import session, Session
from requests.status_codes import codes
from requests.exceptions import (

Additional:
requests is pure python, for installion on WinXp I need this_zip_archive, unzip it, and copy the the inner requests-folder into C:\Program Files (x86)\LibreOffice 4\program\python-core-3.3.3\lib\site-packages\ that works for me so far.

No, unfortunately it didn’t work. I tried before to simply copy the inner request-folder into site-packages, but it failed. I also tried to install pip with get-pip.py, but when I ran that, python.exe crashed.

I have no idea why it doesnt work, what about the first suggestion editing the …__init__.py ??

Yes, i edited the init file and it unfortunately didn’t work. As a workaround I now call my standard python interpreter in a subprocess whenever I want to use 3rd party packages and use the libreoffice interpreter to access the uno api. This seems to work, though I hope it won’t drag down perfomance too much…
Thx alot for trying to help.

If you have a copy of the same major and minor version of Python installed on your system (Python 3.3.x for LO 4.3 and 4.4 current releases) then you essentially have two possible solutions to reach the same goal. If, however, you do not have the same Python branch installed then only one option is reliably available to you.

That solution is to install pip (and setuptools) within the LibreOffice installation and then use the pip program (script) to install anything else. Likewise, calling the full path to the LO version of Python when installing with a setup.py file will do much the same thing.

The alternative method, if you have a regular installation of Python 3.3 or are willing to try it with 3.4 (which might be bad and might break other things, so be careful) then all you need to do is make sure that the LibreOffice Python can access the site-packages directories which exist elsewhere on the system.

Run this script with the system version of Python 3.3:

import sys

afile = open("sites.pth", "w")
for s in sys.path:
    afile.write(s+"\n")
afile.close()

Then copy or move the sites.pth file it creates to a LibreOffice directory which should include something like this: LibreOfficePython.framework/Versions/3.3/lib/python3.3/site-packages/

Note: I’m checking an OS X system here, so there will be path differences, but basically you want to find the site-packages directory for the LO installation and copy the sites.pth file there. That file will be checked by Python for additional locations to check for modules and thus will find the modules installed for your regular Python installation, including requests if it’s there.

Oh, by the way, on an unrelated note, there are better options than ctypes, depending on what you’re doing. Take a look at Cython and CFFI, more likely CFFI.