Python Modules & Packages

About Python Module

In Python, a module is a file containing Python code that defines functions, classes, and variables.

The functions, classes, and variables of a module can be imported and used in other Python programs.

Modules help organize code, promote reusability, and maintain a clean project structure.

There are two types of modules in Python: built-in modules and user-defined modules.

  • Built-in Modules: These are modules that are part of the Python standard library and come pre-installed with Python. They provide a wide range of functionality, such as mathematical operations, file handling, and more. Examples include the math module for mathematical functions and the random module for generating random numbers.
  • User-defined Modules: These are modules that you create yourself to organize your code. You can define your own functions, classes, and variables in a user-defined module and import it into other Python scripts to use its functionality.

We strongly recommend you to practice creating and using both built-in and user-defined modules in Visual Studio Code or in your preferred IDE for better understanding. You can also practice using built-in modules in the Python Editor provided at the end of this tutorial.

Different Ways to Import Modules in Python

There are several ways to import modules in Python, depending on your needs.

  • Import the entire module: This is the most common way to import a module. You can access all functions and variables in the module using the module name as a prefix (e.g., module_name.function_name()).
  • Import specific functions or variables: You can import specific functions or variables from a module using the from ... import ... statement. This allows you to use the imported functions or variables directly without the module name prefix.
  • Import with an alias: You can give a module an alias using the as keyword. This is useful if the module name is long or if you want to avoid naming conflicts. (e.g. , import module_name as alias).)
  • Import all functions and variables: You can import all functions and variables from a module using the from ... import * statement. However, this is not recommended as it can lead to naming conflicts and make your code less readable.
Example of Importing the Entire Module
Example of Importing Specific Functions or Variables
Example of Importing Module with an Alias
Example of Importing All Functions and Variables (not recommended)

Create User-defined Module in Python

Create a new Python file called mymodule.py. The file name will be the module name.

Add some functions and variables to the mymodule.py file. For example:

Save the mymodule.py file in the same directory as your main Python script or in a directory that is included in your Python path.

You have created a user-defined module named mymodule with functions greet, add, and subtract, and a variable PI.

Using User-defined Module in Python

To use the user-defined module mymodule in your main Python script, you need to use the import statement.

Create a new Python file called main.py and add the following code to import and use the functions and variables from the mymodule module:

Example of Using User-defined Module
Explanation of the above example:
  • The import mymodule statement imports the user-defined module named mymodule.
  • The functions greet, add, and subtract are called using the module name as a prefix (e.g., mymodule.greet()).
  • The variable PI is accessed using the module name as a prefix (e.g., mymodule.PI).
  • The output of the program will display the results of the function calls and the value of the variable.

Using Built-in Standard Library Module in Python

Python provides a wide range of built-in modules that you can use in your programs. To use a built-in module, you can also use the import statement.

You can import a built-in module in your Python script and use its functions and variables. For example, the math module provides mathematical functions and constants.

To use the built-in math module, you can do the following:

Example of Using Built-in Math Module
Explanation of the above example:
  • The import math statement imports the built-in module named math.
  • The functions sqrt and factorial are called using the module name as a prefix (e.g., math.sqrt()).
  • The constant pi is accessed using the module name as a prefix (e.g., math.pi).
  • The output of the program will display the results of the function calls and the value of the constant.

About Python Packages of Modules

A package in Python is a way of organizing related modules into a directory hierarchy. It allows you to group related functionality together and provides a namespace for the modules within the package.

Packages can contain modules, __init__ files, and sub-packages. The __init__.py file is used to mark a directory as a package and can also contain initialization code for the package.

You can import modules from a package using the dot notation (e.g., import package_name.module_name).

Create a Package of Python Modules

  • Create a directory for your package with your preferred name as the directory name will be your package name.
  • Inside the package directory, create a file named __init__.py. This file can be empty or can contain initialization code for the package.
  • Create one or more Python files (modules) inside the package directory. Each file will be treated as a module in the package.
  • You can create sub-packages by creating sub-directories inside the main package directory. Each sub-directory must contain its own __init__.py file and modules.
Directory Structure of a Package of Python Modules
Explanation of the above structure:
  • The directory mypackage is the package name.
  • The file __init__.py indicates that mypackage is a package.
  • The files module1.py and module2.py are modules within the package.
Initializing a Package of Python Modules in __init__.py file

You can initialize a package by adding code to the __init__.py file. This code will be executed when the package is imported. You can also use the __init__.py file to define what is included in the package when it is imported using the from package_name import * statement.

Explanation of the above example:
  • The from .module1 import * imports all functions and variables from module1.
  • The from .module2 import specific_function imports a specific function named specific_function from module2.
Importing and Using Modules from a Package
Explanation of the above example:
  • The from mypackage import module1, module2 statement imports the modules module1 and module2 from the package mypackage.
  • The function specific_function() from module2 is called using the module name as a prefix (e.g., module2.specific_function()).

You can also import the entire package and access the modules and their functions using the package name as a prefix (e.g., import mypackage and then mypackage.module1.function_name()).

Python Compiler For Live Practice

Practice the built-in modules in the following Python Editor. We strongly recommend you to practice user-defined modules and packages in the Visual Studio Code or in your preferred IDE for better understanding. You can import Python built-in modules in the following code editor and use their functions and variables and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output