Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, a module is a file containing Python definitions and statements. You can use modules to organize your code, reuse existing code, and share functionality across different scripts or applications. The import
statement is used to load and access the functionality provided by a module.
In this tutorial, we'll cover the basics of importing modules in Python.
To import a module, simply use the import
statement followed by the module name. For example, let's import the built-in math
module:
import math
Once a module is imported, you can access its functions and attributes using the dot (.) notation. For example, let's use the sqrt()
function and pi
constant from the math
module:
import math result = math.sqrt(25) print(result) # Output: 5.0 area = math.pi * (5 ** 2) print(area) # Output: 78.53981633974483
If you only need specific functions or attributes from a module, you can use the from
keyword along with import
. For example, let's import just the sqrt()
function and pi
constant from the math
module:
from math import sqrt, pi result = sqrt(25) print(result) # Output: 5.0 area = pi * (5 ** 2) print(area) # Output: 78.53981633974483
as
:Sometimes, you may want to rename an imported module, function, or attribute to avoid naming conflicts or for better readability. You can use the as
keyword to do so. For example, let's rename the sqrt()
function from the math
module:
from math import sqrt as square_root result = square_root(25) print(result) # Output: 5.0
*
:You can use the wildcard *
to import all functions and attributes from a module. However, this practice is generally discouraged, as it can lead to naming conflicts and make it harder to understand which module a function or attribute comes from. Only use this method if you're sure that there are no naming conflicts.
from math import * result = sqrt(25) print(result) # Output: 5.0 area = pi * (5 ** 2) print(area) # Output: 78.53981633974483
You can also create and import your own modules. Suppose you have a file named my_module.py
in the same directory as your script:
# my_module.py def greet(name): return f"Hello, {name}!"
To import and use the greet()
function from my_module
, simply use the import
statement followed by the module name (without the .py
extension):
import my_module greeting = my_module.greet("John") print(greeting) # Output: Hello, John!
In summary, importing modules in Python allows you to reuse and share functionality across different scripts and applications. You can import built-in modules, third-party modules, or your own modules using the import
statement. Access functions and attributes using the dot (.) notation or import specific functions and attributes using the from
keyword. Rename imports using the as
keyword.
How to import a module in Python:
import
keyword followed by the module name to import a module. Access module attributes using dot notation.import math result = math.sqrt(25) print(result)
Importing specific functions from a module in Python:
from math import sqrt result = sqrt(25) print(result)
Wildcard imports in Python for modules:
*
to import all functions and attributes from a module. Caution: It may lead to namespace pollution.from math import * result = sqrt(25) print(result)
Importing modules from different directories in Python:
sys.path
or use relative imports to import modules from different directories.import sys sys.path.append('/path/to/module') import my_module
Troubleshooting common import errors in Python:
# Incorrect import import non_existing_module
Dynamic module import in Python:
importlib
module to dynamically import modules based on user input or other runtime conditions.import importlib module_name = "math" math_module = importlib.import_module(module_name) result = math_module.sqrt(25) print(result)
Circular imports in Python and how to avoid them:
# module_a.py def function_a(): from module_b import function_b # Use function_b here # module_b.py def function_b(): from module_a import function_a # Use function_a here