在 python 中,我可以输入 import Image
来导入 Python 图像库 (PIL)。我可以直接输入它,而不是像其他人那样 from PIL import Image
。我怎么能做到这一点? from PIL import Image
和 import Image
有什么区别吗?
我应该假设其他人不能做到这一点吗? (即编写代码分发给其他人时。)
我使用Python 2.7.4。下面是在 REPL 中完美运行的代码示例:
> import Image
> x = Image.new("RGB",(32,32),"blue)
> x.save("test.png","PNG")
最佳答案
看起来当您直接“导入 Image”时,该 Image 模块内部真正发生的事情是:“from PIL.Image import *”,它将所有名称从 PIL.Image 模块复制到 Image 模块中。
看一下源码:
In [1]: import Image
In [2]: Image??
Type: module
String form: <module 'Image' from '/usr/lib/python2.7/dist-packages/PILcompat/Image.pyc'>
File: /usr/lib/python2.7/dist-packages/PILcompat/Image.py
Source: from PIL.Image import *
关于python - 为什么我只能 "Import Image"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195011/