- Any Python file is a module, its name being the file's base name
without the
.pyextension. - A package is a collection of Python modules: while a module is a
single Python file, a package is a directory of Python modules
containing an additional
__init__.pyfile, to distinguish a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own__init__.pyfile.
The distinction between module and package seems to hold just at the file system level. When you import a module or a package, the corresponding object created by Python is always of type module. Note, however, when you import a package, only variables/functions/classes in the __init__.py file of that package are directly visible, not sub-packages or modules.
Example
As an example, consider the xml package in the Python standard library: its xml directory contains an __init__.py file and four sub-directories; the sub-directory etree contains an __init__.py file and, among others, an ElementTree.py file.
See what happens when you try to interactively import package/modules:
>>> import xml
>>> type(xml)
<type 'module'>
>>> xml.etree.ElementTree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'etree'
>>>
>>>
>>> import xml.etree
>>> type(xml.etree)
<type 'module'>
>>> xml.etree.ElementTree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ElementTree'
>>>
>>>
>>> import xml.etree.ElementTree
>>> type(xml.etree.ElementTree)
<type 'module'>
>>> xml.etree.ElementTree.parse
<function parse at 0x00B135B0>
NOTE
In Python there also are built-in modules, such as sys, that are written in C, but I don't think you meant to consider those in your question.
- Any Python file is a module, its name being the file's base name
without the
.pyextension. - A package is a collection of Python modules: while a module is a
single Python file, a package is a directory of Python modules
containing an additional
__init__.pyfile, to distinguish a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own__init__.pyfile.
The distinction between module and package seems to hold just at the file system level. When you import a module or a package, the corresponding object created by Python is always of type module. Note, however, when you import a package, only variables/functions/classes in the __init__.py file of that package are directly visible, not sub-packages or modules.
Example
As an example, consider the xml package in the Python standard library: its xml directory contains an __init__.py file and four sub-directories; the sub-directory etree contains an __init__.py file and, among others, an ElementTree.py file.
See what happens when you try to interactively import package/modules:
>>> import xml
>>> type(xml)
<type 'module'>
>>> xml.etree.ElementTree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'etree'
>>>
>>>
>>> import xml.etree
>>> type(xml.etree)
<type 'module'>
>>> xml.etree.ElementTree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ElementTree'
>>>
>>>
>>> import xml.etree.ElementTree
>>> type(xml.etree.ElementTree)
<type 'module'>
>>> xml.etree.ElementTree.parse
<function parse at 0x00B135B0>
NOTE
In Python there also are built-in modules, such as sys, that are written in C, but I don't think you meant to consider those in your question.
A module is a single file (or files) that are imported under one import and used. e.g.
import my_module
A package is a collection of modules in directories that give a package hierarchy.
from my_package.timing.danger.internets import function_of_love
Documentation for modules
Introduction to packages
Difference between modules, packages and libraries in Python
Difference between modules, packages and libraries in Python
How are python packages and modules organized?
Guide to how Python's module system actually works?
What are the key features of Python programming?
Can Python be used for web development?
What type of language is python?
Videos
Over the last few years, I have always interchangeably used these terms - Modules, Packages and Libraries. Last week I sat down to understand the true difference and I am glad I did. I have tried to explain them in my article. Do check it out and let me know your opinions.
Modules vs Packages vs Libraries in Python