Tastypie:XML 方面的使用需要 lxml 和 defusedxml

Tastypie : Usage of the XML aspects requires lxml and defusedxml

当 运行 来自生产服务器的 Tastypie 在开发环境中工作正常时,我收到以下错误:

File "/opt/python/run/venv/lib/python2.7/site-packages/tastypie/serializers.py" in to_xml
446. "Usage of the XML aspects requires lxml and defusedxml.")

Usage of the XML aspects requires lxml and defusedxml.

当我检查包裹时,我可以找到它们:

(venv)[root@prod app]# pip freeze |grep xml
defusedxml==0.4.1
lxml==3.6.0

当我检查从文件 site-packages/tastypie/serializers.py 导入并在生产环境的 Python 控制台中重现它时,它起作用了:

(venv)[root@prod app]# python
Python 2.7.10 (default, Dec  8 2015, 18:25:23) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...     import defusedxml.lxml as lxml
...     from defusedxml.common import DefusedXmlException
...     from defusedxml.lxml import parse as parse_xml
...     from lxml.etree import Element, tostring, LxmlError
... except ImportError:
...     lxml = None
... 
>>> lxml is None
False

to_xml 在 lxml is None 测试中失败:

def to_xml(self, data, options=None):
    """
    Given some Python data, produces XML output.
    """
    options = options or {}

    if lxml is None:
        raise ImproperlyConfigured(
            "Usage of the XML aspects requires lxml and defusedxml.")

    return tostring(self.to_etree(data, options), xml_declaration=True,
        encoding='utf-8')

并且直接调用 to_xml 方法有效:

(venv)[root@prod app]# python
Python 2.7.10 (default, Dec  8 2015, 18:25:23) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tastypie.serializers import Serializer
>>> Serializer().to_xml({"test":"test"})
"<?xml version='1.0' encoding='utf-8'?>\n<response><test>test</test></response>"
>>> 

我试过uninstalling/reinstallinglxmldefusedxml,我试过重新开始制作,欢迎任何更多的想法。

服务器在 AWS Beanstalk RHEL (Amazon Linux) 环境 / Python 2.7.

如果您曾经遇到过这个问题,原因确实是安装该库的路径不在您的 Apache Python 路径中。

我这边的问题是我手动安装了这个包,它进入了 dist-package 目录,而不是自动安装的所有其他包所在的 site-package 目录。