Python Package - Module
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 :
- and functions.
In order to use the definitions defined in a module, you must import a definition.
Articles Related
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
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
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
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.