2017-03-17 222 views
1

我正在做一些圖像處理,並且需要導入兩個模塊,但這些模塊具有相同的類名稱。例如:Python導入兩個具有相同類名稱的模塊

from wand.image import Image 
from PIL import Image 

我使用的方法(S),不幸的是,不包含在這兩個,因此我需要兩個模塊。目前我對這個問題的解決方法是在for循環中重複導入模塊,但這看起來不正確。例如:

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

    from PIL import Image 
    # run code for this module 

有沒有一種方法可以'重命名'或調用使用不同名稱的類/方法?謝謝。

+1

您可以使用'as'運算符。例如'從PIL導入圖像作爲pil_image',然後在代碼中使用'pil_image',而不是'Image'。 –

回答

3

可以使用,例如:

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

或任何其它不同的名稱與as幫助。

0

你可以使用as這個關鍵字來代替別名。

例如from PIL import Image as pil_image,然後在代碼中使用pil_image,而不是僅使用Image

2

正如其他人所說,你可以使用as

另一種可能性是導入模塊,然後從那裏引用類。

import wand 
import PIL 

wand.image.Image() 
PIL.Image() 
相關問題