【问题标题】:xhtml2pdf ImportError - Djangoxhtml2pdf ImportError - Django
【发布时间】:2014-03-31 07:03:01
【问题描述】:

我使用 pip 安装了 xhtml2pdf 以与 Django 一起使用。我收到以下 ImportError:

Reportlab Toolkit Version 2.2 or higher needed

但我有 reportlab 3.0

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

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

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"):

什么给了?

【问题讨论】:

  • 您是否在 virtualenv 中使用了旧版本?
  • 不。我将它安装在我的 virtualenv 中。上面的控制台输出也来自我的 virtualenv。我的 Django 项目只有一个 virtualenv,安装了最新版本的reportlab。

标签: python django pip reportlab xhtml2pdf


【解决方案1】:

在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)

【讨论】:

  • 在 Linux 上使用 virtualenv 时,该文件应该位于类似于 /home/&lt;YOUR_USER_NAME&gt;/.virtualenvs/&lt;YOUR_VIRTUAL_ENV_NAME&gt;/lib/python2.7/site-packages/sx/pisa3/pisa_util.py 的位置。
  • @hanleyhansen 在 Heroku 或生产环境中部署时会怎样?我们应该在服务器上做同样的事情吗?他们没有对此进行修复或更新版本吗?
  • @madtyn 该修复程序在一年前被合并到 master。现在应该可以工作了。
  • @hanleyhansen 我应该从 pip 安装哪些依赖项和版本?也许你知道一些链接,我可以一起寻找这些?
  • @madtyn 你可以等到新版本出来或者使用github上的最新版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多