Python – Modules

Modules in PYthon

A module in Python is a container for codes. So you can defined functions, classes and variables inside a module. Let’s say you have a function you have written. This function contains several lines of code and you want to use them in different files. In this case, you can place this function in a module.

We would cover the following:

  1. How to Create a Module
  2. HowThe import Statement
  3. HowThe from…import Statement
  4. HowLocating Modules
  5. HowThe dir () Function
  6. HowThe globals() and locals() Functions
  7. HowThe reload() Function

 

1. How to create a module

To create a module, you first create a Python file and save it with the name of the module. So if you want to create a module called mycodes, then you would first create a file called mycodes.py. Inside this file you can then write the functions or other codes you want to reuse.

Now, there are modules that are built-in that you can just use. For example, the math module that contains useful functions for performing arithmetic. The random module for working with random numbers and sequences, and lots more.

You can scroll down to the video and watch how this is done.

 

 

2. The import Statement

To use the codes in the module, you simply use the import statement. So if you have created a module called mycodes, the to use it in any file, you must add import mycodes in the new file.

You can also import more than one module. For example,

import mycodes

When an import statement is encountered, then the interpreter searches the current path for the module. If it finds it, then it imports it into the program.

Now, after importing a module, you are free to use the functions available in the module. You simply use them by using this syntax below.

modulename.functionname

 

 

3. The from…import Statement

Let’s say a module contains 100 functions, and you want to use only one of them. You  don’t have to import the whole module. The from…import statement allows you to import just a block of code or function from a module.

So if you have a module called modulename and the name of the function you want to use is functionname, you can used the syntax below

 

from modulename import functioname

So this ensures that you import only the particular function

 

You can also use the from module import * statement. In this case, you are importing all the functions from a module into the current namespace.

 

 

4. Locating Modules

When you use an import statement, the Python interpreter checks for the module in following locations in order.

  • The current directory
  • If the module is not there, then Python searches each directory in the PYTHONPATH shell variable
  • If not found, Python then searches the default path.

The variable that contains the module search path is the sys.path variable. The sys.path variable contains the PYTHONPATH, the current directory as well as the installation-dependent default.

 

The PYTHONPATH variable

The PYTHONPATH is an environment variable used by Python. it contains list of directories that Python searches for modules. You can also change the location of this directory using set PYTHONPATH command.

 

 

5. The dir( ) Function

This is an important built-in function in Python. The dir() function returns an sorted list of strings containing names that are defined in a module.

This list would contain all functions, modules, variables defined inside the specified module. For example, the code below lists the content of the math module.

# import the math built-in module
import math

listofitems = dir(math)
print(listofitems)

 

If you execute the code then you will get the list of items below.

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 
'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 
'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 
'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 
'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 
'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 
'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 
'tanh', 'tau', 'trunc']

 

Note the special variable that begins with double underscore __name__. This is the module’s name. The other string __file__ is the file name where the module was imported from

 

 

6. The globals( ) and the locals( ) Functions

These two functions are used to return the names in the global and local namespace. Though this depends on the location from where the call is made.

If you call locals() from within a function, then it would return all the names that are accessible locally from that function. However if you call globals() from inside a function, then it would return all that are accessible globally from that function.

The return type of globals() and the locals() functions is a dictionary.

 

 

7. The reload() Function

If you import a module into your program, then the code in the top-level part of the module is executed just once. This means that if changes are made to the module, you would not have the updated version of the module.

To solve this you used the reload() function. This function  is used to re-execute the top-level code in a module. So you can have the update module. It runs the import statement again. Then syntax for reload() is:

reload(module)

Here, module is the name of the module you want to reload