Python Package - Module

> Procedural Languages > Python > Python - Grammar (Package | Module)

1 - About

On a file system, package are the directories and modules are the files within this directories.

A module is a file that contains structure definitions including :

In order to use the definitions defined in a module, you must import a definition.


3 - Properties

3.1 - Name

Name of the file.

3.2 - Qualified Name

i.e. the name of the module followed by a dot followed by the short name.

4 - Built-in

Python comes with an extensive library, consisting of components called modules. See Python - Builtin Function

5 - Management

5.1 - Run

With the -m option of python you can run a library module as a Python - Script

Example with pip:

python -m pip --help


To make it executable, you need to add the following if statement at the end

if __name__ == "__main__": # When run the name of the module is "__main__" not the file name

5.2 - Installation

As a module is file within a package, a module is installed through a package. See package installation

5.3 - Creation

You can create your own modules simply by entering the code of your procedure definitions and variable assignments in a python file (.py) whose name consists of the module name you choose.
# This module is saved as
def helloWorld():
    print 'HelloWorld'

The module can itself contain import statements, enabling it to make use of defi nitions from other modules.

5.4 - Import

5.5 - Reimport

When you edit your module (for debugging purpose for instance) after the first load (import), you need to reload it in the console in order to see the changes.

from imp import reload

If you import a speci fic module using the from … import … syntax then you cannot reload it.

6 - How to

Print all function of a module

import math            # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything       # Prints 'em all!
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign',
 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin',
 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

6.2 - Get help

with the built-in help function.

You can move forward by typing f and backward by typing b, and you can quit looking at the documentation by typing q.

6.2.1 - on the module

>>> help (math)
Help on built-in module math:


    This module is always available.  It provides access to the
    mathematical functions defined by the C standard.


        Return the arc cosine (measured in radians) of x.


        Return the hyperbolic arc cosine (measured in radians) of x.


        Return the arc sine (measured in radians) of x.


        Return the hyperbolic arc sine (measured in radians) of x.


        Return the arc tangent (measured in radians) of x.

        atan2(y, x)

        Return the arc tangent (measured in radians) of y/x.
        Unlike atan(y/x), the signs of both x and y are considered.


        Return the hyperbolic arc tangent (measured in radians) of x.

7 - Documentation / Reference