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 or from scipy import spatial, but if I simply import scipy calling scipy.spatial.distance.cosine(...) results in the following error: AttributeError: 'module' object has no attribute 'spatial'.
What is wrong with the second approach?
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 "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'dom' >>> import xml.dom >>> xml.dom <module 'xml.dom' from 'C:\Python27\lib\xml\dom\__init__.pyc'> There's an exception like os.path. (os module itself import the submodule into its namespace)
>>> import os >>> os.path <module 'ntpath' from 'C:\Python27\lib\ntpath.pyc'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With