xhtml2pdf ImportError - Django
Pergunta
Eu instalei xhtml2pdf
usando pip
para utilização com o Django.Estou recebendo o seguinte ImportError:
Reportlab Toolkit Version 2.2 or higher needed
Mas eu tenho reportlab 3.0
>>> import reportlab
>>> print reportlab.Version
3.0
Eu encontrei este bloco catch tente no __init__.py
de 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
Há também outro erro na util.py
:
if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
Não deve que ler algo como:
if not (reportlab.Version[:3] >="2.1"):
O que dá?
Solução
No util.py
edite as seguintes linhas:
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")
E definida como:
if not (reportlab.Version[:3] >="2.1"):
raise ImportError("Reportlab Version 2.1+ is needed!")
REPORTLAB22 = (reportlab.Version[:3] >="2.1")
EDITAR
Enquanto as obras mencionadas acima ele ainda usa literais de seqüência de caracteres para a verificação de versão.Há um pull request no xhtml2pdf
projeto com uma solução mais elegante que compara as versões usando tuplas de números inteiros.Esta é a proposta de solução:
_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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow