1. What is a Python module ?

In this tutorial, we will discover another aspect of the Python language that makes it a very powerful, modular and scalable language: the use of Python modules. In particular, we will learn how to use some very useful predefined modules for a Python programmer.

  • A Python module is simply a file or system files made up of Python code that you can call and use its code without having to copy it.
  • A Python module can contain functions, classes, variables ...
  • A Python module allows you to logically organize your Python code: the grouping of associated code in a module makes the code easier to understand and use.
  • Python Modules allow separation and therefore better organization of the code: indeed, it is common in a project to split its code into different files that will contain coherent parts of the final program to facilitate general understanding of the code, maintenance and teamwork if several people work on the project.

In Python, we can distinguish three main categories of module by classifying them according to their publisher:

    1. Standard modules: that are not part of the language itself  but are automatically integrated by Python;
    2. Modules developed by external developers: that we will be able to use by installing it with the pip utility
    3. Our own modules developed by ourselves.

In all cases, the procedure for using a Python module will be the same.
We will start by describing this procedure and then we will study in the rest of this part some standard modules that you should know.

2. Create your own module

2.1 Creating and importing a module

We will try to create our own Python module named myModule:
1. We create a file named myModule.py
2. We introduce a code of some simple functions on the myModule.py file for example:

 def sum(x , y):
    return x + y
def division(x , y):
    return x/y

3. We then create a python file in the same directory for example testModule to test the module myModule.py (the two files myModule.py and testModule.py can be placed on different directories provided you specify the path of the myModule.py files when it is imported)
4. On the testModule.py file, type the code:

 # We import the entire module
from myModule import *
# We can now use the module functions:
print ("the sum of 7 and 8 is:", sum (7 , 8))
print ("the division of 12 by 3 is:", division (12 , 3))

2.2 Partial import of the module

To use the existing functions in a module, it is not necessary to import the entire module, but just import the functions you need. For example if we need to use only the sum() function, we just import this function:

 # We import the sum() function of the module
from myModule import sum
# We can now use the sum function:
print ("the sum of 7 and 8 is:", sum(7 , 8))

We can also use the following syntax:

# import of the entire module
import myModule
print ("the sum of 7 and 8 is:", myModule.sum (7 , 8))

2.3 Import a module by assigning it an alias name

It is sometimes difficult to always retype the same name of a module whose name is quite long. To overcome this problem, we import the module by assigning it an alias of name.

Exemple

import myModule as my
print ("the sum of 5 and 4 is:", my.sum (7 , 8))

3. Importing and using an external module

So far, we've only seen modules that are in the same directory in the Python file that calls the module, but in general, python modules are stored in other directories and accessible via specific paths. We will deal with a simple example to understand: It is assumed that the file myModule.py is located in a library directory and will therefore be accessible via the path: "/library/myModule.py":

In this case we must specify the path in the import instruction:

 
# we specify the module path
from library.myModule import *
# We can now use the module functions:
print ("the sum of 7 and 8 is:", sum (7 , 8))
print ("the division of 12 by 3 is:", division (12 , 3))

4. Standard Python modules

As we told you at the beginning of this tutorial, we will often import modules created by other developers or modules made available to us by Python itself.
Indeed, there are a large number of pre-designed and ready-to-use modules that come standard with Python. These modules will extend the language and allow us to perform all kinds of operations, in particular thanks to the functions they provide us.
To import a Python module, we will again simply use an import statement as if we were importing one of our modules.

The standard Python modules to know are the following

  1. cgi: module ("Common Gateway Interface") provides elements allowing Python programs to run on HTTP servers;
  2. datetime: module provides classes for simple or more complex manipulation of dates and times;
  3. calendar: module that contains the calendar functions
  4. json: module allows the encoding and decoding of data in JSON format;
  5. math: module provides a set of functions for performing complex mathematical calculations;
  6. os: module provides a portable way to use operating system-dependent features;
  7. pickle: module is used to serialize Python objects;
  8. platforme: module that provides various system informations
  9. random: module implements pseudo-random number generators for different distributions;
  10. re: module provides regular expression operations similar to those found in Perl;
  11. socket: module provides access to the sockets interface which corresponds to a standardized set of communication functions;
  12. sys: module provides access to certain system variables used and maintained by the interpreter, and to functions that interact strongly with it;
  13. urllib.request and urllib.parse: modules allow to open, read and analyze URLs.
  14. virtualenv: module allow you to use a virtual environnement

You can find the complete list of standard Python modules on the official website of Python.

5. Python packages

A Python package is simply a set of several modules grouped together. We will be able to import packages in the same way as modules and access a particular module or element using the syntax:

package_name.module_name.element_name

Younes Derfoufi
my-courses.net

Leave a Reply