Wednesday 27 September 2017

Django Python Web App Development - Python XHTML2PDF Library error FIXED - "ImportError: cannot import name inputstream django" Solved

cannot import name inputstream, Django, Django alirazabhayani, Django Full Stack Development ali raza bhayani, html5lib, inputstream, Python, Python alirazabhayani, xhtml2pdf

While working on a Django Web Application using Python, I recently encountered an error while using a library named xhtml2pdf. This library is a Python library for exporting xhtml to PDF.

While using it in Django Web framework for developing a hassle free web service to export customized HTML templates into PDF, I encountered some error which I had to dig into the library to find the actual problem and its solution. I noticed that this problem of "cannot import name inputstream django" was faced by many people and therefore writing this post to document it. Please note that this proposed solution is workable for Python 2.7 and hasn't been checked on Python v.3.x .

When I installed xhtml2pdf library and used it in Django Web framework, I encountered following error:
 importerror: cannot import name inputstream 
Following trace shows that the error is actually triggered from html5lib which is one of the dependencies of xhtml2pdf:
File "/usr/home/username/virtualenvs/projectname/lib/python2.7/site-packages/xhtml2pdf/", line 17, in 
    from html5lib import treebuilders, inputstream 
ImportError: cannot import name inputstream
Due to broken dependencies of xhtml2pdf and lack of documentation, I had to dig in the module which revealed that this error was coming due to the fact that xhtml2pdf requires a specific version of html5lib to work properly. To solve this issue, I installed html5lib's version 1.0b8 in the following way:
$ pip install html5lib==1.0b8
If you had previously installed html5lib's different version, you will have to uninstall it first by running following command on your terminal or virtual environment:
$ pip uninstall html5lib