Python module import - why are components only available when explicitly imported?

前端 未结 2 1378
粉色の甜心
粉色の甜心 2021-01-04 01:15

I have recently installed scikit-image version 0.11.3. I am using python 2.7.10. When I import the entire module I cannot access the io module.

import skimag         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 01:55

    It's simply the way Python handles modules.

    One reason is that it would make importing one module very slow if cpython needed to scan for submodules, import all of them and then import all of their submodules.

    The other reason is "better be explicit than implicit". Why should Python import everything possible when you only need a small fraction of a package with a complex module hierarchy.

    Instead of from skimage import io you can also write

    import skimage.io
    

    then skimage.io.imread will be found.

提交回复
热议问题