tastype:XML特性的使用需要lxml和defusedxm

2024-03-28 14:08:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从生产服务器运行tastype时遇到以下错误,而它在开发环境中正常工作:

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.

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

^{pr2}$

当我检查从文件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\u 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>"
>>> 

我尝试过卸载/重新安装lxmldefusedxml,我尝试重新启动生产,任何更多的想法都将是非常欢迎的。在

服务器位于AWS Beanstalk RHEL(Amazon Linux)环境/Python2.7上。在


Tags: tofromtestimportnone环境venvusage
1条回答
网友
1楼 · 发布于 2024-03-28 14:08:20

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

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

相关问题 更多 >