Difference between a library and a module...

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Mar 7 18:46:34 EST 2006


Laszlo Zsolt Nagy a écrit :
> sophie_newbie wrote:
> 
>> OK this might seem like a retarded question, but what is the difference
>> between a library and a module?
>>
>> If I do:
>>
>> import string
>>
>> am I importing a module or a library?
>>  
>>
> I'm not a guru, but... I think that modules are things that live inside 
> the Python language. 

a (python) module is two things (depending on the context): either a 
python source file or a (compiled) system library ('something that 
resides on the file  system and contains code', isn't it ?), and (once 
imported by the interpreter) it's representation at runtime as a python 
object.

(snip)
> I have the feeling that a library is usually lives in 
> compiled form, while a python module can be anything that can be 
> 'import'-ed (py file, pyd file or an so file...)

Some python modules are in fact coded in C then compiled as system 
librairies (.so on *n*x, .dll on Windows). So there's no clear technical 
distinction here.

AFAIK, "librairy" originally refers to system libs, but we also talk 
about the "standard python library", which is a collection of Python (or 
system lib) modules and packages.


> By the way, modules are not callable at all.
> Methods can only be called with an object.
> Class methods can be called with a class.
> 
> Well, a module is itself a special object, called the 'module object'. 
> Module objects have no class,

Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on 
linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import deco
 >>> deco
<module 'deco' from 'deco.py'>
 >>> deco.__class__
<type 'module'>
 >>> deco.__class__.__name__
'module'

Seems like they do have one...

> and they cannot be instantiated
 >>> deco.__class__('foo')
<module 'foo' (built-in)>
 >>> import types
 >>> types.ModuleType('bar')
<module 'bar' (built-in)>

> 
> I hope this helps.
> 
So do I !-)



More information about the Python-list mailing list