Module, Package

Sharan Basappa sharan.basappa at gmail.com
Tue May 8 13:37:01 EDT 2018


On Tuesday, 8 May 2018 13:05:58 UTC+5:30, Steven D'Aprano  wrote:
> On Mon, 07 May 2018 09:53:45 -0700, Sharan Basappa wrote:
> 
> > I am a bit confused between module and package in Python. Does a module
> > contain package or vice versa? When we import something in Python, do we
> > import a module or a package?
> 
> The term "module" in Python has multiple meanings:
> 
> - a particular kind of object, types.ModuleType
> 
> - a single importable .py, .pyc etc file
> 
> A package is a logical collection of importable .py etc files, usually 
> collected inside a single directory. When you import a module of a 
> package, that gives you a module object.
> 
> Normally we would say that packages contain modules. For example, if you 
> have this file structure:
> 
> 
> library/
> +-- __init__.py   # special file which defines a package
> +-- widgets.py
> +-- stuff/
>     +-- __init__.py
>     +-- things.py
> 
> 
> then we have a package "library", which in turn contains a submodule 
> "library.widgets", and a subpackage "library.stuff", which in turn 
> contains a submodule "library.stuff.things".
> 
> Each of these lines imports a module object:
> 
> import library
> import library.stuff
> import library.stuff.things
> import library.widgets
> 
> from library import widgets
> from library.stuff import things
> 
> 
> Effectively, "packages" relates to how you arrange the files on disk; 
> "modules" relates to what happens when you import them.
> 
> 
> -- 
> Steve

Wow! Thanks a lot.



More information about the Python-list mailing list