XHTML2PDF IMPORTERROR - DJANGO
Domanda
Ho installato xhtml2pdf
usando pip
per l'uso con Django.Sto ricevendo il seguente importatoreRorer:
Reportlab Toolkit Version 2.2 or higher needed
.
ma ho reportlab 3.0
>>> import reportlab
>>> print reportlab.Version
3.0
.
Ho trovato questo blocco di cattura prova nel __init__.py
di 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
.
C'è anche un altro errore nel util.py
:
if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
.
non dovrebbe leggere qualcosa come:
if not (reportlab.Version[:3] >="2.1"):
.
Cosa dà?
Soluzione
in util.py
Modifica le seguenti righe:
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 impostato su:
if not (reportlab.Version[:3] >="2.1"):
raise ImportError("Reportlab Version 2.1+ is needed!")
REPORTLAB22 = (reportlab.Version[:3] >="2.1")
.
modifica
Mentre i lavori sopra riportati utilizzano ancora letterali di stringa per il controllo della versione.C'è una richiesta di tiro nel progetto xhtml2pdf
con una soluzione più elegante che confronta le versioni usando Tuples of Interers.Questa è la soluzione proposta:
_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)
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow