Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python importing two modules that have the same class name

I'm doing some image processing and have the need to import two modules, but those modules have same class name. Example:

from wand.image import Image  
from PIL import Image

The method(s) I'm using, unfortunately, are not contained in both, hence my need for the two modules. Currently my workaround to this problem is import the modules over and over again in a for loop, but this seems incorrect. Example:

for my_images in images:
   from wand.image import Image
   # run code for this module

   from PIL import Image
   # run code for this module

Is there a way I can 'rename' or call the class/method using a different name? Thanks.

like image 700
billz Avatar asked Dec 19 '22 08:12

billz


2 Answers

You can use, for example:

from wand.image import Image as Image_wand
from PIL import Image as Image_PIL

or any another different names with a help of as.

like image 124
Dmitry Avatar answered Mar 05 '23 18:03

Dmitry


As others have said you could use as.

Another possibility is importing the modules, then referencing the classes from there.

import wand
import PIL

wand.image.Image()
PIL.Image()
like image 45
alxwrd Avatar answered Mar 05 '23 18:03

alxwrd