10

我安装了用于 Django 的使用xhtml2pdfpip我收到以下 ImportError:

Reportlab Toolkit Version 2.2 or higher needed

但我有reportlab 3.0

>>> import reportlab
>>> print reportlab.Version                                                                                                                                                                                                                 
3.0

__init__.py我在of中发现了这个 try catch 块xhtml2pdf

REQUIRED_INFO = """
****************************************************
IMPORT ERROR!
%s
****************************************************

The following Python packages are required for PISA:
- Reportlab Toolkit >= 2.2 <http://www.reportlab.org/>
- HTML5lib >= 0.11.1 <http://code.google.com/p/html5lib/>

Optional packages:
- pyPDF <http://pybrary.net/pyPdf/>
- PIL <http://www.pythonware.com/products/pil/>

""".lstrip()

log = logging.getLogger(__name__)

try:
    from xhtml2pdf.util import REPORTLAB22

    if not REPORTLAB22:
        raise ImportError, "Reportlab Toolkit Version 2.2 or higher needed"
except ImportError, e:
    import sys

    sys.stderr.write(REQUIRED_INFO % e)
    log.error(REQUIRED_INFO % e)
    raise

中还有另一个错误util.py

if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):

那不应该是这样的:

if not (reportlab.Version[:3] >="2.1"):

是什么赋予了?

4

1 回答 1

18

util.py编辑以下行:

if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
    raise ImportError("Reportlab Version 2.1+ is needed!")

REPORTLAB22 = (reportlab.Version[0] == "2" and reportlab.Version[2] >= "2")

并设置为:

if not (reportlab.Version[:3] >="2.1"):
    raise ImportError("Reportlab Version 2.1+ is needed!")

REPORTLAB22 = (reportlab.Version[:3] >="2.1")

编辑

虽然上述方法有效,但它仍然使用字符串文字进行版本检查。项目中有一个拉取请求,它xhtml2pdf提供了一个更优雅的解决方案,该解决方案使用整数元组来比较版本。这是建议的解决方案:

_reportlab_version = tuple(map(int, reportlab.Version.split('.')))
if _reportlab_version < (2,1):
    raise ImportError("Reportlab Version 2.1+ is needed!")

REPORTLAB22 = _reportlab_version >= (2, 2)
于 2014-02-27T18:23:25.650 回答