Module, Package
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue May 8 03:33:21 EDT 2018
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
More information about the Python-list
mailing list