1

I installed python-docx and when importing docx, I get some errors. I am using a MAC, Python 3.11, PyCharm. Does anyone knows how to solve this? Btw, I am a beginner in coding and in Python... please explain as you would to a four year old XD.

I don't know if it is a Mac error, or an lxml error, or this file error: etree.cpython-311-darwin.so... any help will be much appreciated.

Here are the commands and errors I get:

import docx
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/__init__.py", line 3, in <module>
    from docx.api import Document  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/api.py", line 14, in <module>
    from docx.package import Package
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/package.py", line 9, in <module>
    from docx.opc.package import OpcPackage
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/opc/package.py", line 9, in <module>
    from docx.opc.part import PartFactory
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/opc/part.py", line 12, in <module>
    from .oxml import serialize_part_xml
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../venv/lib/python3.11/site-packages/docx/opc/oxml.py", line 12, in <module>
    from lxml import etree
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: dlopen(/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so, 0x0002): tried: '/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' (code signature in <C969C6B5-4EEB-3E5B-92FB-9F4148B6A87D> '/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' not valid for use in process: Trying to load an unsigned library), '/System/Volumes/Preboot/Cryptexes/OS/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' (no such file), '/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' (code signature in <C969C6B5-4EEB-3E5B-92FB-9F4148B6A87D> '/Users/.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' not valid for use in process: Trying to load an unsigned library)

I tried to import docx to manipulate Words files. I installed python-docx instead of docx since I am running Python 3.11.

Bencho Mat
  • 11
  • 1

1 Answers1

0

This appears to be an issue with how the venv is set up. I would suggest switching to the system interpreter and ditching virtual environments all together. You can read more about that here.

Pycharms venvs can be super great, but at a low-scale home programming level, they are unnecessary and cause problems like this. After which, go into the terminal and type python3 -m pip install python-docx (or however you have python alias'd), it will automatically install lxml and your import docx should work.

ajgrinds
  • 186
  • 11
  • 1
    Thank you, I switched to system interpreter but I still get errors trying to access venv. Any clue? `ImportError: dlopen(.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so, 0x0002): tried: .../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' (no such file), '.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' (code signature in '.../venv/lib/python3.11/site-packages/lxml/etree.cpython-311-darwin.so' not valid for use in process: Trying to load an unsigned library)` – Bencho Mat May 09 '23 at 21:48