"from module import data; print(data)" vs "import module; print(module.data)"

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Feb 25 17:20:14 EST 2016


sohcahtoa82 at gmail.com wrote:
> Now, I've noticed people talking about importing os.path.  Is there any
> reason to use "import os.path" rather than "import os"?  Both of them will
> still put the "os" module into the global namespace.

In the case of os.path it doesn't matter, because the
os module imports the appropriate path module automatically.

But in general, importing a package won't necessarily
import submodules under it, so sometimes you need to
import somepackage.somemodule explicitly.

-- 
Greg



More information about the Python-list mailing list