Python Package - Module

Card Puncher Data Processing

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.

Properties

Name

Name of the file.

Qualified Name

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

Built-in

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

Management

Run

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

Example with pip:

python -m pip --help

Doc

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
    myMainFunc(sys.argv[1])

Installation

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

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

def helloWorld():

    print 'HelloWorld'

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

Import

See Python Package - Import

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.

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']

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.

on the module

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

NAME
    math

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

FUNCTIONS
    acos(...)
        acos(x)

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

    acosh(...)
        acosh(x)

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

    asin(...)
        asin(x)

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

    asinh(...)
        asinh(x)

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

    atan(...)
        atan(x)

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

    atan2(...)
        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.

    atanh(...)
        atanh(x)

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

Documentation / Reference





Discover More
Card Puncher Data Processing
Language - (Import|Include|Require)

import basically makes names (generally of function) available to the current script. To load modules, language uses different expression: imports includes. require (Node) The import functionality...
Card Puncher Data Processing
Python - Builtin Function

Builtin function are function that doesn't need an import statement. The builtins namespace associated with the execution of a code block is actually found by looking up the name __builtins__ in its...
Card Puncher Data Processing
Python - Compiled File (Cache)

The compiled file of Python. To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory The name where: - the format of the compiled file...
Card Puncher Data Processing
Python - Grammar (Package | Module)

in Python On a file system: package are the directories and modules are the files within this directories. what ?? Python predecessor ABC_(programming_language)ABC. Python_syntax_and_semantics...
Card Puncher Data Processing
Python - Module Search Path (PYTHONPATH and sys.path)

The path is where Python searches the modules to Import. The current path is automatically included. On Windows add the script dir: PYTHONPATH is an environment variable, that governs the sequence...
Card Puncher Data Processing
Python - Name

in Python Names (refer to|are pointer for) objects. Names are introduced by name binding operations such as during the def process. targets that are identifiers if occurring in an assignment,...
Card Puncher Data Processing
Python - Namespace

in Python. The namespace for a module is automatically created the first time a module is imported.
Card Puncher Data Processing
Python - Namespace Package

Namespace packages allow you to split the sub-packages and modules within a single package across multiple, separate distribution packages And you use this package in your code like so: Then you...
Card Puncher Data Processing
Python - Object

in Python. In Python everything is an object including: modules, classes and functions Comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types...
Card Puncher Data Processing
Python - Package

in Python Physically, package are the directories and modules are the files within this directories. Logically, a package is a module (parent module): which can contain: modules (submodules)...



Share this page:
Follow us:
Task Runner