[reportlab-users] Package imports fail

Robin Becker robin at reportlab.com
Fri Feb 3 05:53:09 EST 2012


On 02/02/2012 23:11, Brett Davis wrote:

> Hi ReportLab users,

>

> The following pairs of imports fail silently on my machine:

> ==

> import scipy

> import reportlab.platypus

> ==

> import Image

> import reportlab.platypus

> ==

> import scipy

> import reportlab.pdfgen.canvas

> ==

> import Image

> import reportlab.pdfgen.canvas

> ==


first off it's not exactly clear what silently fail means, but looking at the
stackoverflow issue and your use of the reportlab.lib.utils.Image implis it
must be some kind of import ambiguity. I'm guessing it is related to the
following python behaviour




> >>>> from PIL import Image as PilImage

> >>> import Image

> >>> Image is PilImage

> False

> >>>


ie we appear to have two ways to import PIL. Python appears to have no way to
detect that the two import paths resolve to the same module so they are
different and then objects derived from them are also not the same. In our code
we are using this to try and import the module

try:
from PIL import Image
except ImportError:
try:
import Image
except ImportError:
Image = None
haveImages = Image is not None

ie we first try and import the absolute way and then try the PIL folder is on
the path route. When PIL is installed it suffers from having two routes to the
Image modules so this ambiguity can occur. I don't know where the fault actually
lies, but at least we are trying to first import from an unambiguous package
name. Given that Image is quite likely to be overworked as an identifier that's
probably the correct way, but you may see it differently.

A hack fix might be to try both versions of Image in the _isPilImage function,
but that might mean we are using two versions of Image.Image simultaneously.

Try rewriting _isPilImage like

def _isPilImage(im):
try:
from PIL import Image
try:
r = isinstance(im,Image.Image)
except:
r = False
except ImportError:
r = False
if r: return r
try:
import Image
try:
r = isinstance(im,Image.Image)
except:
r = False
except ImportError:
r = False
return r0 or r

and see if that makes a difference for your failures. Probably we ought to think
of some non-module way to detect usage of the PIL Image type; ideas welcome.


> This means that I can't use ReportLab in conjunction with SciPy, and to use

> it with the Imaging Library, I have to use Image from the

> reportlab.lib.utils namespace. Does anyone know why this happens? I found

> the PIL workaround here:

> http://stackoverflow.com/questions/2227493/reportlab-and-python-imaging-library-images-from-memory-issuebut

> I haven't found anything for SciPy, and I'm not getting any error

> information, so I don't even know if it's the same problem.

>

> I'm using Python 2.6.2 on Windows XP Professional with Service Pack 3,

> SciPy 0.7.1 on top of NumPy 1.3.0, PIL 1.1.7, and ReportLab 2.5. It would

> be really nice if anyone could shed some light on this.

>

> Thanks,

> Brett

>

.....

--
Robin Becker


More information about the reportlab-users mailing list