Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a Python module and a Python package?

What's the difference between a Python module and a Python package?

See also: What's the difference between "package" and "module" (for other languages)

like image 775
Dave Avatar asked Oct 30 '11 22:10

Dave


People also ask

What is the difference between Python’s module package and library?

What is the difference between Python’s Module, Package and Library? Module: The module is a simple Python file that contains collections of functions and global variables and with having a .py extension file. It is an executable file and to organize all the modules we have the concept called Package in Python.

What is a package in Python?

A package is a collection of Python modules, i.e., a package is a directory of Python modules containing an additional __init__.py file. The __init__.py distinguishes a package from a directory that just happens to contain a bunch of Python scripts.

How do you describe a Python module?

That is the easiest way to describe it. Module - A Module refers to a file. The file has script 'in it' and the name of the file is the name of the module, Python files end with .py. All the file contains is code that ran together makes something happen, by using functions, strings ect.

How to import a module into a package in Python?

Import module named demo_module and call myModule function inside it. Package: The package is a simple directory having collections of modules. This directory contains Python modules and also having __init__.py file by which the interpreter interprets it as a Package. The package is simply a namespace.


Video Answer


2 Answers

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

like image 21
Jakob Bowyer Avatar answered Oct 18 '22 22:10

Jakob Bowyer


Any Python file is a module, its name being the file's base name without the .py extension. 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__.py file, 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__.py file.

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. 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> 

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.

like image 172
Giulio Piancastelli Avatar answered Oct 18 '22 22:10

Giulio Piancastelli