Failing to install python-docx due to failed building wheel for lxml

Python version : Python 3.12.3
macOS Sonoma 14.4.1

I was trying to install python-docx and encountered the following error message regarding lxml.

   Collecting python-docx
  Using cached python_docx-1.1.1-py3-none-any.whl.metadata (2.0 kB)
Collecting lxml<=4.9.2,>=3.1.0 (from python-docx)
  Using cached lxml-4.9.2.tar.gz (3.7 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Collecting typing-extensions>=4.9.0 (from python-docx)
  Using cached typing_extensions-4.11.0-py3-none-any.whl.metadata (3.0 kB)
Using cached python_docx-1.1.1-py3-none-any.whl (242 kB)
Using cached typing_extensions-4.11.0-py3-none-any.whl (34 kB)
Building wheels for collected packages: lxml
  Building wheel for lxml (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for lxml (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [176 lines of output]
      <string>:117: SyntaxWarning: invalid escape sequence '.'
      <string>:67: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
      Building lxml version 4.9.2.
      Building without Cython.
      Building against libxml2 2.9.4 and libxslt 1.1.29
      Building against libxml2/libxslt in the following directory: /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-10.9-universal2-cpython-312
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/_elementpath.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/sax.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/pyclasslookup.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/__init__.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/builder.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/doctestcompare.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/usedoctest.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/cssselect.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/ElementInclude.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/__init__.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/soupparser.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/defs.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/_setmixin.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/clean.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/_diffcommand.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/html5parser.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/__init__.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/formfill.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/builder.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/ElementSoup.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/_html5builder.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/usedoctest.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      copying src/lxml/html/diff.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/html
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron
      copying src/lxml/isoschematron/__init__.py -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron
      copying src/lxml/etree.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/etree_api.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/lxml.etree.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/lxml.etree_api.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/objectify.pyx -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/etree.pyx -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/public-api.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xmlid.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/cleanup.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xslt.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xpath.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/debug.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/serializer.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/classlookup.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/saxparser.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/objectpath.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/relaxng.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xinclude.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/iterparse.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/parser.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/dtd.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/apihelpers.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xmlerror.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/readonlytree.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/extensions.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xmlschema.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/schematron.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/docloader.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/nsclasses.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/parsertarget.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/xsltext.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/proxy.pxi -> build/lib.macosx-10.9-universal2-cpython-312/lxml
      copying src/lxml/includes/xmlerror.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/c14n.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/xmlschema.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/__init__.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/schematron.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/tree.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/uri.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/etreepublic.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/xpath.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/htmlparser.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/xslt.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/config.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/xmlparser.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/xinclude.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/dtdvalid.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/relaxng.pxd -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/lxml-version.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      copying src/lxml/includes/etree_defs.h -> build/lib.macosx-10.9-universal2-cpython-312/lxml/includes
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/rng
      copying src/lxml/isoschematron/resources/rng/iso-schematron.rng -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/rng
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl
      copying src/lxml/isoschematron/resources/xsl/XSD2Schtrn.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl
      copying src/lxml/isoschematron/resources/xsl/RNG2Schtrn.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl
      creating build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_abstract_expand.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_dsdl_include.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_skeleton_for_xslt1.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_message.xsl -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/readme.txt -> build/lib.macosx-10.9-universal2-cpython-312/lxml/isoschematron/resources/xsl/iso-schematron-xslt1
      running build_ext
      building 'lxml.etree' extension
      creating build/temp.macosx-10.9-universal2-cpython-312
      creating build/temp.macosx-10.9-universal2-cpython-312/src
      creating build/temp.macosx-10.9-universal2-cpython-312/src/lxml
      clang -fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -arch arm64 -arch x86_64 -g -DCYTHON_CLINE_IN_TRACEBACK=0 -I/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include -Isrc -Isrc/lxml/includes -I/Library/Frameworks/Python.framework/Versions/3.12/include/python3.12 -c src/lxml/etree.c -o build/temp.macosx-10.9-universal2-cpython-312/src/lxml/etree.o -w -flat_namespace
      src/lxml/etree.c:262394:36: error: no member named 'curexc_traceback' in 'struct _ts'
              PyObject* tmp_tb = tstate->curexc_traceback;
                                 ~~~~~~  ^
      src/lxml/etree.c:262397:21: error: no member named 'curexc_traceback' in 'struct _ts'
                  tstate->curexc_traceback = tb;
                  ~~~~~~  ^
      src/lxml/etree.c:263340:53: error: no member named 'ob_digit' in 'struct _longobject'
              const digit* digits = ((PyLongObject*)op1)->ob_digit;
                                    ~~~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:263457:53: error: no member named 'ob_digit' in 'struct _longobject'
              const digit* digits = ((PyLongObject*)op1)->ob_digit;
                                    ~~~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:265182:53: error: no member named 'ob_digit' in 'struct _longobject'
              const digit* digits = ((PyLongObject*)op1)->ob_digit;
                                    ~~~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:267536:55: error: no member named 'gi_code' in 'PyGenObject'
          if (PyGen_CheckExact(obj) && ((PyGenObject*)obj)->gi_code && ((PyCodeObject *)((PyGenObject*)obj)->gi_code)->co_flags & CO_ITERABLE_COROUTINE) {
                                       ~~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:267536:104: error: no member named 'gi_code' in 'PyGenObject'
          if (PyGen_CheckExact(obj) && ((PyGenObject*)obj)->gi_code && ((PyCodeObject *)((PyGenObject*)obj)->gi_code)->co_flags & CO_ITERABLE_COROUTINE) {
                                                                                        ~~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:268588:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:268643:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:268897:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:268952:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269093:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269148:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269289:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269344:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269523:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269578:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269757:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      src/lxml/etree.c:269812:55: error: no member named 'ob_digit' in 'struct _longobject'
                  const digit* digits = ((PyLongObject*)x)->ob_digit;
                                        ~~~~~~~~~~~~~~~~~~  ^
      fatal error: too many errors emitted, stopping now [-ferror-limit=]
      20 errors generated.
      Compile failed: command '/usr/bin/clang' failed with exit code 1
      creating var
      creating var/folders
      creating var/folders/rg
      creating var/folders/rg/44f312ss6jddp8pnjhvd8qqw0000gn
      creating var/folders/rg/44f312ss6jddp8pnjhvd8qqw0000gn/T
      cc -I/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include -I/usr/include/libxml2 -c /var/folders/rg/44f312ss6jddp8pnjhvd8qqw0000gn/T/xmlXPathInit8gw85dwa.c -o var/folders/rg/44f312ss6jddp8pnjhvd8qqw0000gn/T/xmlXPathInit8gw85dwa.o
      cc var/folders/rg/44f312ss6jddp8pnjhvd8qqw0000gn/T/xmlXPathInit8gw85dwa.o -L/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/lib -lxml2 -o a.out
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for lxml
Failed to build lxml
ERROR: Could not build wheels for lxml, which is required to install pyproject.toml-based projects

I updated my macOS system to the latest. Ensured Xcode is installed. Tried to uninstall and reinstall lxml. But the same error persists. Anyone encountered the same?

New contributor

anqi cheng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật