V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
TenTo
V2EX  ›  Python

关于 Python 的 import 问题

  •  
  •   TenTo · 2017-10-10 22:50:03 +08:00 via iPhone · 2281 次点击
    这是一个创建于 2397 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在用 PIL 的时候,需要这样 import:
    from PIL import Image
    im = Image()

    但是为什么不能:
    import PIL
    im = PIL.Image()

    查了一些资料,都解释的不太清楚。有谁能分享一下如何理解吗?
    第 1 条附言  ·  2017-10-11 05:19:13 +08:00

    不好意思,提问的时候有些信息没有提供完全。

    使用的是Python3.6。之前的代码示例也有些问题。

    在用 PIL 的时候,需要这样 import:

    from PIL import Image 
    im = Image.open("xxx")
    ``
    
    但是为什么不能: 
    
    

    import PIL im = PIL.Image.open("xxx")

    
    看过`PIL`包下的`__init__.py`文件,确实没有导入`Image`。
    
    8 条回复    2017-10-12 20:36:58 +08:00
    264768502
        1
    264768502  
       2017-10-10 23:11:47 +08:00 via iPad
    难道不是 import Image?

    Pillow?
    PIL 是一个目录(包),它不是一个类或者方法,所以不存在 PIL.Image()这个东西
    stebest
        2
    stebest  
       2017-10-10 23:21:55 +08:00 via Android
    你知道__future__不,通常 2 的版本会有 import print_function. 和这个一样,你需要具体到特定的函数,直接 import PIL 结果是什么也不引入。
    keysona
        3
    keysona  
       2017-10-10 23:23:49 +08:00   ❤️ 1
    https://github.com/python-pillow/Pillow/blob/master/PIL/__init__.py

    看看它的__init__.py 文件吧。

    它那里并没有帮你导入 Image,所以需要 from xxx import xxx 这样导入。
    TenTo
        4
    TenTo  
    OP
       2017-10-11 05:21:15 +08:00
    @keysona #3,确实如你所说,`Python`在导入的时候是根据`__init__.py`来决定哪些模块是导入包的时候就导入的。感谢。
    Morriaty
        5
    Morriaty  
       2017-10-11 09:56:24 +08:00   ❤️ 1
    > when you import a package, only variables/functions/classes in the __init__.py file of that package are directly visible, not sub-packages or modules.
    scriptB0y
        6
    scriptB0y  
       2017-10-11 10:19:02 +08:00
    Python 的 package 相当于一个名字空间(一种特殊的 module 类型),import package 会调用 __import__() 搜索到该 package 然后执行下面的 __init__.py ,将最后得到的对象和一个名字绑定在一起。也就是你得到的 PIL。所以你得到的这个对象并没有 Image。

    但是我觉得

    ```
    from PIL import Image
    import PIL

    im = PIL.Image.open("xxx")
    ```

    就没有问题了。具体看一下文档 https://docs.python.org/3/reference/import.html#packages 应该就没什么疑问了。
    yangtukun1412
        7
    yangtukun1412  
       2017-10-11 11:41:31 +08:00
    import PIL.Image
    Tianny
        8
    Tianny  
       2017-10-12 20:36:58 +08:00
    @Morriaty awesome
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1681 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:15 · PVG 00:15 · LAX 09:15 · JFK 12:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.