0

When I do import docx with the python-docx module, I get this error:

Traceback (most recent call last):
  File "/Userpath/script.py", line 19, in <module>
    import docx
  File "/Library/Python/2.7/site-packages/docx/__init__.py", line 3, in <module>
    from docx.api import Document  # noqa
  File "/Library/Python/2.7/site-packages/docx/api.py", line 14, in <module>
    from docx.package import Package
  File "/Library/Python/2.7/site-packages/docx/package.py", line 11, in <module>
    from docx.opc.package import OpcPackage
  File "/Library/Python/2.7/site-packages/docx/opc/package.py", line 12, in <module>
    from .part import PartFactory
  File "/Library/Python/2.7/site-packages/docx/opc/part.py", line 12, in <module>
    from .oxml import serialize_part_xml
  File "/Library/Python/2.7/site-packages/docx/opc/oxml.py", line 12, in <module>
    from lxml import etree
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml/etree.so, 2): Library not loaded: libxslt.1.dylib
  Referenced from: /Library/Python/2.7/site-packages/lxml/etree.so
  Reason: unsafe use of relative rpath libxslt.1.dylib in /Library/Python/2.7/site-packages/lxml/etree.so with restricted binary

Any idea what's going on here?

scanny
  • 26,423
  • 5
  • 54
  • 80
adamcircle
  • 694
  • 1
  • 10
  • 26

1 Answers1

2

This is a problem with your lxml installation.

I expect you get the same answer in Python when you type in the line:

>>> from lxml import etree

Which means it's not related to python-docx, it's just trying to load a package that's not (or not properly) installed.

Best bet is to search on 'windows install lxml' and learn what you can, but the quick fix might be to uninstall whatever lxml you have and reinstall it from scratch using a binary distribution from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Yes, the same error occurs through the command line, but even after uninstalling and reinstalling lxml, the issue persists. I'm on Mac 10.11, for reference. – adamcircle Jun 26 '17 at 14:13
  • Could be not having libxml2 and libxslt libraries installed: https://stackoverflow.com/a/26544099/1902513 – scanny Jun 26 '17 at 19:16
  • This, too, did not solve the issue :(. Both of those libraries installed successfully and without errors, but the original error associated with lxml has not changed. – adamcircle Jun 27 '17 at 13:59
  • Hmm, it seems this is a new problem. There are some indications of what it might be if you search on 'lxml unsafe use of relative rpath libxslt.1.dylib'. Apparently some new security measures on OS X 10.7 or so. This GitHub comment indicates adding a symlink might work if you can interpolate from the mysql case: https://github.com/brianmario/mysql2/issues/646#issuecomment-140141504 – scanny Jun 27 '17 at 20:40
  • Also you should probably make sure the `lxml` guys know about this. The long-term fix will be a change they make in their code I believe. I've added the `lxml` tag to your question. One other direction you could pursue is running in a virtualenv. If you're using the default OS X install of Python, that's in a protected area (directory owned by root) and could be contributing. Another option would be installing the homebrew Python so it's in a local (to you) directory rather than `/Library/...`. – scanny Jun 27 '17 at 20:42
  • My stopgap solution has been to just use a different computer, because I need this to work for my job, but I would obviously prefer if this would work on my primary system. Is there any other information I can gather that would help to solve this issue, or otherwise aid the lxml devs in fixing it? – adamcircle Jun 27 '17 at 21:32
  • Well, if I were them, I'd like to know whether installing in a non-root-owned Python installation worked. I expect it will. That would be the Python you installed with homebrew, for example. It's wise practice not to fool around with the default OS X Python installation because it's used by Mac system maintenance scripts, and because it's owned by `root` you have to use `sudo` to add or remove packages (in fact, using `sudo -H pip install lxml` might actually work, but I wouldn't recommend it :). I'd either try a non-root installation or a virtualenv and include the results you found. – scanny Jun 28 '17 at 01:10