Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 291
Paul Baltescu Avatar asked Jan 12 '14 05:01

Paul Baltescu


1 Answers

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'> 
like image 119
falsetru Avatar answered Sep 27 '22 23:09

falsetru