简体   繁体   English

需要帮助在os x 10.7上安装lxml

[英]Need help installing lxml on os x 10.7

I have been struggling to be able to do from lxml import etree ( import lxml works fine by the way) The error is: 我一直在努力能够from lxml import etreeimport lxml顺便工作)错误是:

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-            packages/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so

i used pip to install lxml, and homebrew to reinstall libxml2 with the right architecture (or so i think) ...does anyone have ideas on how to fix/diagnose this? 我使用pip来安装lxml,并使用正确的架构重新安装libxml2(或者我认为)...有没有人有关于如何修复/诊断这个的想法? I'm on 64 bit python 我是64位python

lxml is a bit fussy about what 3rd-party libraries it uses and it often needs newer versions than what are supplied by Apple. lxml对于它使用的第三方库有点挑剔,并且它通常需要比Apple提供的版本更新的版本。 Suggest you read and follow the instructions here for building lxml from source on Mac OS X including building its own statically linked libs. 建议您阅读并按照此处的说明在Mac OS X上从源代码构建lxml ,包括构建自己的静态链接库。 That should work. 这应该工作。 (I'm a little surprised that homebrew doesn't already have an lxml recipe.) (我有点意外的是,自制软件还没有lxml配方。)

UPDATE: Based on the limited information in your comments, it is difficult to be sure exactly what is happening. 更新:根据您评论中的有限信息,很难确切地知道发生了什么。 I suspect you are not using the version of Python you think you are. 我怀疑你没有使用你认为的Python版本。 There are any number of ways to install lxml successfully; 有多种方法可以成功安装lxml; that's part of the problem: there are too many options. 这是问题的一部分:有太多的选择。 Rather than trying to debug your setup, here's probably the simplest way to get a working lxml on 10.7 using the Apple-supplied system Python 2.7. 而不是试图调试你的设置,这可能是使用Apple提供的系统Python 2.7在10.7上获得工作lxml的最简单方法。

$ sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml

You should then be able to use lxml.etree this way: 然后你应该能够以这种方式使用lxml.etree

$ /usr/bin/python2.7
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> etree.__file__
'/Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so'
>>> 

I notice though that the lxml static build process does not produce a working universal build. 我注意到lxml静态构建过程不会产生有效的通用构建。 You'll probably see messages like this during the lxml install: 你可能会在lxml安装过程中看到这样的消息:

ld: warning: ignoring file /private/tmp/easy_install-83mJsV/lxml-2.3.1/build/tmp/libxml2/lib/libxslt.a, file was built for archive which is not the architecture being linked (i386)

Assuming the default architecture on your machine is 64-bits, if you try to run in 32-bit mode: 假设您的计算机上的默认体系结构是64位,如果您尝试以32位模式运行:

$ arch -i386 /usr/bin/python2.7
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:06) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so
>>> ^D

And there is the error message you originally reported! 并且您最初报告的错误消息! So the root cause of that appears to be that the static libraries ( libxml2 etc) that lxml builds are not universal. 所以它的根本原因似乎是lxml构建的静态库( libxml2等)不是通用的。 As long as you have no need to use lxml in a 32-bit process (unlikely for most uses), this should not be a problem. 只要您不需要在32位进程中使用lxml (大多数用途不太可能),这应该不是问题。 Chances are that the Python you were originally using was a 32-bit-only one; 有可能你最初使用的Python是一个只有32位的Python; that is consistent with some of the other messages you reported. 这与您报告的其他一些消息一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM