在 M1 mac 上安装 lxml 时出现问题

Aco*_*orn 7 python lxml pip libxml2 apple-m1

所以,我在安装 lxml 时遇到了经典的麻烦。

\n

最初我只是进行 pip 安装,但是当我尝试使用释放内存时Element.clear()出现以下错误:

\n
Python(58695,0x1001b4580) malloc: *** error for object 0x600000bc3f60: pointer being freed was not allocated\n
Run Code Online (Sandbox Code Playgroud)\n

我认为这一定是因为 lxml 正在使用系统的 libxml2,它可能已经过时了。

\n

所以我使用 homebrew 安装 libxml2 和 libxlt,并强制链接它们。

\n

然后我尝试使用以下命令进行安装:

\n
\xe2\x9d\xaf STATIC_DEPS=true pip install lxml --no-cache-dir                                                13:01:46\nCollecting lxml\n  Downloading lxml-4.8.0.tar.gz (3.2 MB)\n     \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81 3.2/3.2 MB 5.4 MB/s eta 0:00:00\n  Preparing metadata (setup.py) ... done\nBuilding wheels for collected packages: lxml\n  Building wheel for lxml (setup.py) ... done\n  Created wheel for lxml: filename=lxml-4.8.0-cp310-cp310-macosx_12_0_arm64.whl size=1683935 sha256=47912c1ba66d274c3ad7b2a2db00243f96d334a3fd5e439725f5005a7a72a602\n  Stored in directory: /private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-ephem-wheel-cache-4_v4ov7s/wheels/e4/52/34/64064e2e2f1ce84d212a6dde6676f3227846210a7996fc2530\nSuccessfully built lxml\nInstalling collected packages: lxml\nSuccessfully installed lxml-4.8.0\n
Run Code Online (Sandbox Code Playgroud)\n

..但是当我尝试导入 etree 时,我会收到此错误:

\n
Traceback (most recent call last):\n  File "/Users/human/Code/ia_book_images/viewer/book_image_downloader.py", line 4, in <module>\n    from lxml import etree as ET\nImportError: dlopen(/Users/human/.virtualenvs/ia_book_images/lib/python3.10/site-packages/lxml/etree.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '___htmlDefaultSAXHandler'\n
Run Code Online (Sandbox Code Playgroud)\n

所以我想让我们 100% 确保它使用 CFLAGS 使用正确版本的 libxml2 并得到以下结果:

\n
\xe2\x9d\xaf CFLAGS="-I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include" STATIC_DEPS=true pip install lxml --no-cache-dir\nCollecting lxml\n  Downloading lxml-4.8.0.tar.gz (3.2 MB)\n     \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81 3.2/3.2 MB 4.4 MB/s eta 0:00:00\n  Preparing metadata (setup.py) ... error\n  error: subprocess-exited-with-error\n\n  \xc3\x97 python setup.py egg_info did not run successfully.\n  \xe2\x94\x82 exit code: 1\n  \xe2\x95\xb0\xe2\x94\x80> [199 lines of output]\n      Checking for gcc...\n      Checking for shared library support...\n      Building shared library libz.1.2.12.dylib with gcc.\n      Checking for size_t... Yes.\n      Checking for off64_t... No.\n      Checking for fseeko... Yes.\n      Checking for strerror... Yes.\n      Checking for unistd.h... Yes.\n      Checking for stdarg.h... Yes.\n      Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().\n      Checking for vsnprintf() in stdio.h... Yes.\n      Checking for return value of vsnprintf()... Yes.\n      Checking for attribute(visibility) support... Yes.\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -I. -c -o example.o test/example.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o adler32.o adler32.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o crc32.o crc32.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o deflate.o deflate.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o infback.o infback.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o inffast.o inffast.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o inflate.o inflate.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o inftrees.o inftrees.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o trees.o trees.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o zutil.o zutil.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o compress.o compress.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o uncompr.o uncompr.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o gzclose.o gzclose.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o gzlib.o gzlib.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o gzread.o gzread.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN  -c -o gzwrite.o gzwrite.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -I. -c -o minigzip.o test/minigzip.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/adler32.o adler32.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/crc32.o crc32.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/deflate.o deflate.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/infback.o infback.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/inflate.o inflate.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/inffast.o inffast.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/inftrees.o inftrees.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/trees.o trees.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/zutil.o zutil.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/gzclose.o gzclose.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/uncompr.o uncompr.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/compress.o compress.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/gzlib.o gzlib.c\n      libtool -o libz.a adler32.o crc32.o deflate.o infback.o inffast.o inflate.o inftrees.o trees.o zutil.o compress.o uncompr.o gzclose.o gzlib.o gzread.o gzwrite.o\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/gzread.o gzread.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN  -DPIC -c -o objs/gzwrite.o gzwrite.c\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -o example example.o -L. libz.a\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -o minigzip minigzip.o -L. libz.a\n      gcc -dynamiclib -install_name /private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/build/tmp/libxml2/lib/libz.1.dylib -compatibility_version 1 -current_version 1.2.12 -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -fPIC -DHAVE_HIDDEN -o libz.1.2.12.dylib adler32.lo crc32.lo deflate.lo infback.lo inffast.lo inflate.lo inftrees.lo trees.lo zutil.lo compress.lo uncompr.lo gzclose.lo gzlib.lo gzread.lo gzwrite.lo  -lc -arch x86_64\n      ld: warning: ignoring file crc32.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file adler32.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file deflate.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file infback.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file inffast.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file inflate.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file inftrees.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file trees.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file compress.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file zutil.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file uncompr.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file gzread.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file gzlib.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file gzclose.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      ld: warning: ignoring file gzwrite.lo, building for macOS-x86_64 but attempting to link with file built for unknown-arm64\n      rm -f libz.dylib libz.1.dylib\n      ln -s libz.1.2.12.dylib libz.dylib\n      ln -s libz.1.2.12.dylib libz.1.dylib\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -o examplesh example.o -L. libz.1.2.12.dylib\n      gcc -I/opt/homebrew/opt/libxslt/include -I/opt/homebrew/opt/libxml2/include -DHAVE_HIDDEN -o minigzipsh minigzip.o -L. libz.1.2.12.dylib\n      ld: warning: ignoring file libz.1.2.12.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64\n      ld: warning: ignoring file libz.1.2.12.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64\n      Undefined symbols for architecture arm64:\n        "_gzclose", referenced from:\n            _gz_compress in minigzip.o\n            _gz_uncompress in minigzip.o\n        "_gzdopen", referenced from:\n            _main in minigzip.o\n        "_gzerror", referenced from:\n            _gz_compress in minigzip.o\n            _gz_uncompress in minigzip.o\n        "_gzopen", referenced from:\n            _file_compress in minigzip.o\n            _file_uncompress in minigzip.o\n            _main in minigzip.o\n        "_gzread", referenced from:\n            _gz_uncompress in minigzip.o\n        "_gzwrite", referenced from:\n            _gz_compress in minigzip.o\n      ld: symbol(s) not found for architecture arm64\n      Undefined symbols for architecture arm64:\n        "_compress", referenced from:\n            _test_compress in example.o\n           (maybe you meant: _test_compress)\n        "_deflate", referenced from:\n            _test_deflate in example.o\n            _test_large_deflate in example.o\n            _test_flush in example.o\n            _test_dict_deflate in example.o\n           (maybe you meant: _test_large_deflate, _test_deflate , _test_dict_deflate )\n        "_deflateEnd", referenced from:\n            _test_deflate in example.o\n            _test_large_deflate in example.o\n            _test_flush in example.o\n            _test_dict_deflate in example.o\n        "_deflateInit_", referenced from:\n            _test_deflate in example.o\n            _test_large_deflate in example.o\n            _test_flush in example.o\n            _test_dict_deflate in example.o\n        "_deflateParams", referenced from:\n            _test_large_deflate in example.o\n        "_deflateSetDictionary", referenced from:\n            _test_dict_deflate in example.o\n        "_gzclose", referenced from:\n            _test_gzio in example.o\n        "_gzerror", referenced from:\n            _test_gzio in example.o\n        "_gzgetc", referenced from:\n            _test_gzio in example.o\n        "_gzgets", referenced from:\n            _test_gzio in example.o\n        "_gzopen", referenced from:\n            _test_gzio in example.o\n        "_gzprintf", referenced from:\n            _test_gzio in example.o\n        "_gzputc", referenced from:\n            _test_gzio in example.o\n        "_gzputs", referenced from:\n            _test_gzio in example.o\n        "_gzread", referenced from:\n            _test_gzio in example.o\n        "_gzseek", referenced from:\n            _test_gzio in example.o\n        "_gztell", referenced from:\n            _test_gzio in example.o\n        "_gzungetc", referenced from:\n            _test_gzio in example.o\n        "_inflate", referenced from:\n            _test_inflate in example.o\n            _test_large_inflate in example.o\n            _test_sync in example.o\n            _test_dict_inflate in example.o\n           (maybe you meant: _test_large_inflate, _test_inflate , _test_dict_inflate )\n        "_inflateEnd", referenced from:\n            _test_inflate in example.o\n            _test_large_inflate in example.o\n            _test_sync in example.o\n            _test_dict_inflate in example.o\n        "_inflateInit_", referenced from:\n            _test_inflate in example.o\n            _test_large_inflate in example.o\n            _test_sync in example.o\n            _test_dict_inflate in example.o\n        "_inflateSetDictionary", referenced from:\n            _test_dict_inflate in example.o\n        "_inflateSync", referenced from:\n            _test_sync in example.o\n        "_uncompress", referenced from:\n            _test_compress in example.o\n        "_zlibCompileFlags", referenced from:\n            _main in example.o\n        "_zlibVersion", referenced from:\n            _main in example.o\n      clang: error: linker command failed with exit code 1 (use -v to see invocation)\n      ld: symbol(s) not found for architecture arm64\n      clang: error: linker command failed with exit code 1 (use -v to see invocation)\n      make: *** [minigzipsh] Error 1\n      make: *** Waiting for unfinished jobs....\n      make: *** [examplesh] Error 1\n      Traceback (most recent call last):\n        File "<string>", line 2, in <module>\n        File "<pip-setuptools-caller>", line 34, in <module>\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/setup.py", line 270, in <module>\n          **setup_extra_options()\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/setup.py", line 162, in setup_extra_options\n          ext_modules = setupinfo.ext_modules(\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/setupinfo.py", line 74, in ext_modules\n          XML2_CONFIG, XSLT_CONFIG = build_libxml2xslt(\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/buildlibxml.py", line 428, in build_libxml2xslt\n          cmmi(zlib_configure_cmd, zlib_dir, multicore, **call_setup)\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/buildlibxml.py", line 352, in cmmi\n          call_subprocess(\n        File "/private/var/folders/g9/lqph46sj36n9kkvjt1pzdxhm0000gn/T/pip-install-kl4hmrrk/lxml_4ecb3c255ad049e39a89a66ee0a50e76/buildlibxml.py", line 335, in call_subprocess\n          raise Exception('Command "%s" returned code %s' % (cmd_desc, returncode))\n      Exception: Command "make -j6" returned code 2\n      Building lxml version 4.8.0.\n      Latest version of zlib is 1.2.12\n      Downloading zlib into libs/zlib-1.2.12.tar.gz from https://zlib.net/zlib-1.2.12.tar.gz\n      Unpacking zlib-1.2.12.tar.gz into build/tmp\n      Latest version of libiconv is 1.16\n      Downloading libiconv into libs/libiconv-1.16.tar.gz from https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz\n      Unpacking libiconv-1.16.tar.gz into build/tmp\n      Latest version of libxml2 is 2.9.12\n      Downloading libxml2 into libs/libxml2-2.9.12.tar.gz from http://xmlsoft.org/sources/libxml2-2.9.12.tar.gz\n      Unpacking libxml2-2.9.12.tar.gz into build/tmp\n      Latest version of libxslt is 1.1.34\n      Downloading libxslt into libs/libxslt-1.1.34.tar.gz from http://xmlsoft.org/sources/libxslt-1.1.34.tar.gz\n      Unpacking libxslt-1.1.34.tar.gz into build/tmp\n      Starting build in build/tmp/zlib-1.2.12\n      [end of output]\n\n  note: This error originates from a subprocess, and is likely not a problem with pip.\nerror: metadata-generation-failed\n\n\xc3\x97 Encountered error while generating package metadata.\n\xe2\x95\xb0\xe2\x94\x80> See above for output.\n\nnote: This is an issue with the package mentioned above, not pip.\nhint: See above for details.\n
Run Code Online (Sandbox Code Playgroud)\n

我需要做一些特殊的事情才能在 m1 mac 上构建 lxml 吗?

\n

Aco*_*orn -1

事实证明,用简单的方法安装 lxml工作pip install 很好。

我的 malloc 错误的原因是我试图在看到结束标记之前清除该元素。事实证明这是不可能的,即使您已经知道您对该元素不感兴趣,您也需要等待结束标记。