Module
A module in Python is a file containing Python code, which can include definitions of functions, classes, variables, and runnable statements. The file name of the module is the module name with a .py extension appended.
You can create your own module by saving Python code in a .py file. For example, a file named mymodule.py might contain:
You can then use this module in another Python script by importing it with the import statement:
When using functions or variables from a module, you refer to them with the syntax module_name.function_name or module_name.variable_name.
Python also includes many built-in modules, such as math or platform, which you can import and use directly.
In summary:
- A module is a
.pyfile containing Python code (functions, classes, variables). - Modules help organize code and promote reuse.
- You import modules using the
importkeyword. - You access module contents using dot notation (
module_name.item).
This modular approach is fundamental in Python programming for building scalable and maintainable codebases.
What is a Module in Python?
In Python, a module is simply a file containing Python code. It can define functions, classes, and variables, and it can also include runnable code. Modules allow you to organize code into separate files and reuse code across multiple programs.
Example:
A file named math_utils.py:
You can use it in another script by importing it:
How to Know if Something is a Module
There are several ways to determine if something is a module:
1. Check the Type
Use the built-in type() function:
2. Use inspect Module
Python's inspect module can be used to check more precisely:
3. File Extension
A module is typically a .py file. When you import something and it points to a .py file or a compiled .pyc file (or a built-in module), itβs a module.
You can also find where a module comes from:
Built-in modules like sys or math might not have a __file__ because they are compiled into the Python interpreter.
Summary
- A module is a
.pyfile or built-in component containing Python definitions and code. - Use
type(obj),inspect.ismodule(obj), or check for__file__to identify if something is a module. - Modules help organize and reuse code.
In Python, modules and packages are both tools for organizing code, but theyβre slightly different:
Module vs. Package β What's the Difference?
| Feature | Module | Package |
|---|---|---|
| Definition | A single .py file |
A folder containing __init__.py and modules/subpackages |
| Example | math.py, os.py |
numpy, pandas, myapp/ |
| Purpose | Organizes related code in one file | Organizes multiple modules/files in a directory |
| Import Example | import math |
import numpy.linalg |
π How to Tell If It's a Module or Package?
You can identify them in several ways:
β
1. Use type() or inspect
These won't directly tell you "module or package," but help confirm if something is a module:
To go further...
β
2. Check for __path__ Attribute
Only packages have the __path__ attribute.
import os
print(hasattr(os, '__path__')) # False β it's a module
import numpy
print(hasattr(numpy, '__path__')) # True β it's a package
β
3. Check __file__ Path
You can inspect the __file__ path to see if it's a single .py file (module) or a directory (package):
print(os.__file__) # ends in os.py β module
print(numpy.__file__) # path within numpy/ β package
β 4. Explore the Filesystem
- If it's a single
.pyfile β it's a module. - If it's a directory with an
__init__.pyfile (even if empty) β it's a package.
π Summary
| Check | Module | Package |
|---|---|---|
type(obj) |
<class 'module'> |
<class 'module'> |
hasattr(obj, '__path__') |
False |
True |
__file__ |
Ends in .py |
Path inside a folder |
| Filesystem | Single .py file |
Folder with __init__.py |