Why does from scipy import spatial work, while scipy.spatial doesn't work after import scipy?

前端 未结 3 445
野趣味
野趣味 2020-12-11 14:41

I would like to use scipy.spatial.distance.cosine in my code. I can import the spatial submodule if I do something like import scipy.spatial<

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 15:30

    Importing a package does not import submodule automatically. You need to import submodule explicitly.

    For example, import xml does not import the submodule xml.dom

    >>> import xml
    >>> xml.dom
    Traceback (most recent call last):
      File "", line 1, in 
    AttributeError: 'module' object has no attribute 'dom'
    >>> import xml.dom
    >>> xml.dom
    
    

    There's an exception like os.path. (os module itself import the submodule into its namespace)

    >>> import os
    >>> os.path
    
    

提交回复
热议问题