10

I installed xhtml2pdf using pip for use with Django. I am getting the following ImportError:

Reportlab Toolkit Version 2.2 or higher needed

But I have reportlab 3.0

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

I found this try catch block in the __init__.py of 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

There's also another error in the util.py:

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

Shouldn't that read something like:

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

What gives?

2
  • are you may be using an older version within a virtualenv ?
    – karthikr
    Feb 27, 2014 at 17:16
  • Nope. I installed it within my virtualenv. Also the console output above is from my virtualenv. I only have one virtualenv for my Django projects with the latest version of reportlab installed. Feb 27, 2014 at 17:17

1 Answer 1

18

In util.py edit the following lines:

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

And set to:

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

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

EDIT

While the above works it still uses string literals for version checking. There's a pull request in the xhtml2pdf project with a more elegant solution that compares versions using tuples of integers. This is the proposed solution:

_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)
10
  • 3
    The file should be found in location similar to this /home/<YOUR_USER_NAME>/.virtualenvs/<YOUR_VIRTUAL_ENV_NAME>/lib/python2.7/site-packages/sx/pisa3/pisa_util.py when using virtualenv on Linux.
    – andilabs
    Oct 29, 2014 at 16:00
  • @hanleyhansen What about when coming to deploy in Heroku or in a production environment? Should we do the same in the server? Haven't they made a fix or a newer version with this fixed?
    – madtyn
    Mar 3, 2015 at 22:58
  • @madtyn The fix was merged to master over a year ago. Should be working now. Mar 3, 2015 at 23:03
  • @hanleyhansen Which dependencies and versions should I install from pip for ? Maybe you know some link where I can look for this all together?
    – madtyn
    Mar 3, 2015 at 23:06
  • 1
    @madtyn you can either wait until a new version comes out or use the latest version on github Mar 4, 2015 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.