Packages, modules, libraries,...

Adrian Eyre a.eyre at optichrome.com
Tue Feb 1 08:50:37 EST 2000


> I've seen the words package, module, and library, used in this group 
> frequently. I'm the naive type of programmer, so I thought that these
> were synonyms. Now, some people seem to use these words as such, but
> then there are others who seem to distinguish between them. What is
> true? Are perhaps both views true in some sense? Could someone please
> enlighten me.

I'm not sure about "library", but "package" and "module" are well
defined in Python.

Imagine the following filesystem structure:

/home/fred/python/	<- This is in the PYTHONPATH
                 |
                 +- cafe/
                 |      |
                 |      +- __init__.py
                 |      |
                 |      +- bacon.py
                 |
                 +- spam.py


import spam             # Imports "module" from file spam.py[c]
import cafe             # Imports "package" from file cafe/__init__.py[c]
import cafe.bacon       # Imports "module" from file cafe/bacon.py[c]

Packages are really useful in that they allow you to easily create
arbitrarily nested namespaces in Python, whilst maintaing a easily
managed directory structure.

The presence of an __init__.py in a directory constitutes a package,
although technically, a package is just a module:

>>> import spam
>>> spam
<module 'spam' from '/home/fred/python/spam.py'>
>>> import cafe
>>> cafe
<module 'cafe' from '/home/fred/python/cafe/__init__.py'>
>>> import cafe.bacon
>>> cafe.bacon
<module 'cafe.bacon' from '/home/fred/python/cafe/bacon.py'>
>>>

-----------------------------------------------------------------
Adrian Eyre <a.eyre at optichrome.com> - http://www.optichrome.com 





More information about the Python-list mailing list