Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python Modules

A module allows you to logically organize your Python code.

Grouping related code into a module makes the code easier to understand and use.

A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables.

A module can also include runnable code.


The import Statement

You can use any Python source file as a module by executing an import statement in some other Python source file.

The import has the following syntax:

Syntax:
import moduleName

# OR

import moduleName as alias

When the interpreter encounters an import statement, it imports the module if the module is present in the search path.

A search path is a list of directories that the interpreter searches before importing a module.

For example, to import the module random.py, you need to put the following command at the top of the script:

A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening repeatedly, if multiple imports occur.


The from...import Statement

Python's from statement lets you import specific attributes from a module into the current namespace.

The from...import has the following syntax:

Syntax:
from moduleName import name1, name2, ...

This statement does not import the entire module, it just import the given name from module.

Note: When you use from statement, you don't need to explicitly type module name before calling functions.

It is also possible to import all the names from a module into the current namespace by using the following import statement.

Syntax:
from moduleName import *


Executing Modules as Scripts

Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

The code in the module will be executed, just as you imported it.

But with the __name__ set to "__main__" you can sepcify a block that will not executed when a module is executed.

When you execute code below it will print Hello world:

echo.py

When you execute code below it will not print Hello world.

This is because we set condition if __name__ == "__main__": in echo.py and this condition will not be true if you import this file.

main.py

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences:

  • The current directory.
  • If the module is not found, Python then searches each directory in the shell variable PYTHONPATH.
  • If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python3/.

The module search path is stored in the system module sys as the sys.path variable.

The sys.path variable contains the current directory, PYTHONPATH, and 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.


The globals() and locals() Functions

The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called.

  • If locals() is called from within a function, it will return all the names that can be accessed locally from that function.
  • If globals() is called from within a function, it will return all the names that can be accessed globally from that function.

The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.


The reload() Function

When a module is imported into a script, the code in the top-level portion of a module is executed only once.

Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function.

The reload() function imports a previously imported module again.

Here is a list of all Built-in Python 3 Modules