gpt4 book ai didi

python - 如何在 Yosemite 上设置 lxml 和 pypy?

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:00 24 4
gpt4 key购买 nike

我想学习一些 lxml 和 pypy,所以我决定在我的 Yosemite Mac 上安装它。但经过三天的尝试,我仍然无法尝试 lxml,因为我无法正确设置。

这是我所做的:

  1. 是否安装了干净的homebrewxcode-select --install

    proix:~ user$ brew --version
    0.9.5

    proix:~ user$ gcc --version
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
    Target: x86_64-apple-darwin14.0.0
    Thread model: posix
  2. 编译了 libxml2libxslt - libxml2 代码 tar 3.4.1 和 libxslt 代码 tar 1.1.28;工作得很好。库已构建并安装。

    proix:~ user$ brew list
    libxml2 libxslt

    proix:~ user$ brew info
    2 kegs, 409 files, 14M

    proix:~ user$ ll /usr/local/Cellar/libxml2/2.9.2/lib/
    total 6096
    drwxr-xr-x 8 user admin 272 27 Dez 11:46 .
    drwxr-xr-x 13 user admin 442 27 Dez 11:46 ..
    drwxr-xr-x 3 user admin 102 27 Dez 11:46 cmake
    -r--r--r-- 1 user admin 1184284 27 Dez 11:46 libxml2.2.dylib
    -r--r--r-- 1 user admin 1922024 27 Dez 11:46 libxml2.a
    lrwxr-xr-x 1 user admin 15 27 Dez 11:46 libxml2.dylib -> libxml2.2.dylib
    drwxr-xr-x 3 user admin 102 27 Dez 11:46 pkgconfig
    -r--r--r-- 1 user admin 269 27 Dez 11:46 xml2Conf.sh

    proix:~ user$ ll /usr/local/Cellar/libxslt/1.1.28/lib/
    total 1440
    drwxr-xr-x 10 user admin 340 27 Dez 12:10 .
    drwxr-xr-x 13 user admin 442 27 Dez 12:10 ..
    -r--r--r-- 1 user admin 76728 27 Dez 12:10 libexslt.0.dylib
    -r--r--r-- 1 user admin 101832 27 Dez 12:10 libexslt.a
    lrwxr-xr-x 1 user admin 16 27 Dez 12:10 libexslt.dylib -> libexslt.0.dylib
    -r--r--r-- 1 user admin 214344 27 Dez 12:10 libxslt.1.dylib
    -r--r--r-- 1 user admin 326040 27 Dez 12:10 libxslt.a
    lrwxr-xr-x 1 user admin 15 27 Dez 12:10 libxslt.dylib -> libxslt.1.dylib
    drwxr-xr-x 4 user admin 136 27 Dez 12:10 pkgconfig
    -r--r--r-- 1 user admin 288 27 Dez 12:10 xsltConf.sh
  3. 但是这些新版本并未被使用:

    $ xmllint --version
    xmllint: using libxml version 20900
  4. 因此,我通过恢复控制台(启动期间 cmd+R)切换了 /usr/lib 下的库。重新启动后,我得到了预期的结果:

    $ xmllint --version
    xmllint: using libxml version 20902`

    警告!不要尝试在正常登录 session 期间执行此操作。如果操作系统无法再找到 libxml2.dylib,它会让您的系统完全无用。

  5. 创建一个用于测试的 virtualenv:

    virtualenv lxmllab
    source lxmllab/bin/activate`
  6. 使用STATIC_DEPS=true sudo pip install lxml安装lxml。效果也很好:

    (lxmllab)proix:~ user$ pip list
    backports.ssl-match-hostname (3.4.0.2)
    certifi (14.5.14)
    cffi (0.6)
    docutils (0.12)
    ipython (2.3.1)
    Jinja2 (2.7.3)
    lxml (3.4.1)
    MarkupSafe (0.23)
    nose (1.3.4)
    numpydoc (0.5)
    pip (6.0.3)
    py (1.4.26)
    Pygments (2.0.1)
    pyzmq (14.4.1)
    setuptools (8.2.1)
    Sphinx (1.2.3)
    tornado (4.0.2)
  7. 测试一下:

    (lxmllab)proix:~ user$ pypy -c 'from lxml import etree'
    Unknown libxml2 version: 20902
    Traceback (most recent call last):
    File "app_main.py", line 72, in run_toplevel
    File "app_main.py", line 562, in run_it
    File "<string>", line 1, in <module>
    File "lxml.etree.pyx", line 270, in init lxml.etree (src/lxml/lxml.etree.c:199039)
    File "lxml.etree.pyx", line 235, in lxml.etree.__unpackDottedVersion (src/lxml/lxml.etree.c:9383)
    TypeError: unsupported operand type for int(): 'unicode'

    (lxmllab)proix:~ user$ pypy
    Python 2.7.3 (5acfe049a5b0, May 21 2013, 13:47:22)
    [PyPy 2.0.2 with GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    And now for something completely different: ``redefining yellow seems like a
    better idea''
    ---- from lxml import etree
    Unknown libxml2 version: 20902
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "lxml.etree.pyx", line 270, in init lxml.etree (src/lxml/lxml.etree.c:199039)
    File "lxml.etree.pyx", line 235, in lxml.etree.__unpackDottedVersion (src/lxml/lxml.etree.c:9383)
    TypeError: unsupported operand type for int(): 'unicode'
    ----

这就是我陷入困境的地方。我尝试了一些修复,但没有成功:

  • LD_LIBRARY_PATH 和/或 DYLD_LIBRARY_PATH 设置为 libxml2 的位置。
  • 将 libxml2 dylib 复制到 virtualenv site-packages/lxml 文件夹。

有谁知道我应该做什么才能让它工作,或者让 lxml 库在 Yosemite 下工作的正确方法是什么?

最佳答案

PyPy 不能与 lxml 一起使用(至少不是很好,即使它意外地可以),因为 lxml 是在使用 CPython C API 绑定(bind)的 Cython 之上构建的。考虑使用 lxml-cffi 代替 https://github.com/amauryfa/lxml/tree/cffi

关于python - 如何在 Yosemite 上设置 lxml 和 pypy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com