- Python modules contain different statements and in addition functions defined inside.
- In other words, the module can define functions, classes, and variables. Therefore, Modules help in organizing the code making it easier to use and understand. In addition, Modules provide the reusability of the code.
How to create a python modules
- the import statement:
You can use any Python source file as a module by executing an import statement in some other Python source file.
# Import module support
# Now you can call defined function that module as follows
Hello : Zara
- the from-import statement:
Python’s from statement lets you import specific attributes from a module into the current namespace.
from fib import fibonacci
When you import a module, the Python interpreter searches for the module in the following sequences −
- The current directory.
- If the module isn’t found, therefore ,Python then searches each directory in the shell variable PYTHONPATH.
- If all else fails, Python checks the default path. On UNIX, therefore, this default path is normally /usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.pathvariable. therefore ,The sys.path variable contains the current directory, PYTHONPATH, and in addition the installation-dependent default.
The dir( ) Function
The dir() built-in function returns a sorted list of strings containing the names defined by a module.
In addition ,the list contains the names of all the modules, variables and functions that are defined in a module .
content = dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',
'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']
Import modules as object
Python modules can be imported as objects. In such case, instead of
Python modules search path
When a module is imported, Interpreter first searches for a built-in module with that name. If it is not found in the list of built-in modules, then the interpreter searched for the module in the following locations in order.
In addition to it,
- The current working directory.
- The directories of
. It is an environment variable with the list of directories.
- The standard installation path of Python – the installation dependent default.
Import everything a sa module
Besides importing certain functions, we can import everything defined inside the module and use the functions directly in the program.
The reload() function
As we have already stated that, a module is loaded once regardless of the number of times it is imported into the python source file. However, if you want to reload the already imported module to re-execute the top-level code, python provides us the reload() function.
Scope of variables
we have two types of scopes. All the variables defined in a module that have the global scope unless it is define in a function.
All the variable define in a function have a local scope that is limited to this function itself. We can not access a local variable globally.
If two variables are defined with the same name with the two different scopes, i.e., local and global, then the priority will always be given to the local variable.
The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. therefore ,The packages are to categorize the application level code efficiently.
Let’s create a package named Employees in your home directory. Consider the following steps.
1. Create a directory with name Employees on path /home.
2. Create a python source file with name ITEmployees.py on the path /home/Employees.
3. Similarly, create one more python file with name BPOEmployees.py and create a function getBPONames().
4. Now therefore, the directory Employees which we have created in the first step contains two python modules. To make this directory a package, we need to include one more file here, that is __init__.py which contains the import statements of the modules defined in this directory.
5. Now in addition , the directory Employees has become the package containing two python modules. Here in addition, we must notice that we must have to create __init__.py inside a directory to convert this directory to a package.
6. therefore ,To use the modules defined inside the package Employees, we must have to import this in our python source file. therefore ,Let’s create a simple python source file at our home directory (/home) which uses the modules defined in this package.
['John', 'David', 'Nick', 'Martin']
in addition,We can have sub-packages inside the packages. We can nest the packages up to any level depending upon the application requirements.