Importing in python

Importing is the process of bringing code from one module (or package) into another module, allowing you to access and use the functions, classes, and variables defined in the imported module. Python provides various ways to import code:

Basic Import
The basic form of importing a module is using the import keyword followed by the module name:
import mymodule
Alias Import
You can provide an alias (a shorter name) for a module using the as keyword:
import mymodule as mm
result = mm.greet("Bob")
print(result) # Output: Hello, Bob!
Relative Imports
In larger projects with multiple packages and modules, you can use relative imports to import code from modules in the same package or from parent packages:
# Assuming this is within a package
from . import sibling_module
from .. import parent_module

This allows you to use the imported items without the module prefix.

Python's Standard Library
Python comes with a vast standard library that includes modules for a wide range of tasks, such as file handling, math operations, networking, and more. These modules are available for use without the need for additional installation:
import math
square_root = math.sqrt(16)
print(square_root) # Output: 4.0

Circular Imports

Be cautious when dealing with circular imports, where modules import each other in a circular manner. This can lead to unexpected behavior and should be avoided or carefully managed.

Modules in Python are files containing Python code, including functions, classes, and variables, that can be reused in other Python scripts or programs. Modules help you organize and compartmentalize your code, making it more maintainable and promoting code reuse.

Modules in python

Creating a Module
To create a module, you save a Python script as a .py file. This script can contain functions, classes, or variable definitions. Here’s an example of creating a module:
# mymodule.py
def greet(name):
return f"Hello, {name}!"
pi = 3.14159
Using Modules
To use code from a module in another script, you need to import the module using the import statement:
# main.py
import mymodule

result = mymodule.greet("Alice")
print(result) # Output: Hello, Alice!

Module Namespace
Functions, variables, and classes defined in a module belong to the module's namespace. To access them, you prefix their names with the module name, separated by a dot (.):
import mymodule
result = mymodule.greet("Alice")
pi_value = mymodule.pi
print(result) # Output: Hello, Alice!
print(pi_value) # Output: 3.14159

Best practices

  • Use Meaningful Names: Use meaningful module and package names that reflect the purpose of the code.

  • Organize Related Code: Organize related functions, classes, and variables into separate modules.

  • Avoid Wildcard Imports: Avoid wildcard imports (from module import *) to maintain code clarity.

  • Document Your Code: Document your modules and functions using docstrings to make them more understandable.

  • ConsistentStructure: Use a consistent and organized project directory structure for larger projects.

Example project structures

Here’s an example of how you might structure a Python project with multiple modules and packages:

my_project/
├── main.py
├── mymodule.py
├── package/
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
│ └── subpackage/
│ ├── __init__.py
│ └── submodule.py

In main.py, you can import and use the modules as needed:

import mymodule
from package import module1
from package.subpackage import submodule
result = mymodule.greet("David")
print(result) # Output: Hello, David!

Quiz Question

True or False. Modules in Python can contain only functions, not classes or variables.

Quiz Question

What is the correct way to import a function named greet from a module named mymodule?

Quiz Question

What will be the output of the following code if mymodule.py contains pi = 3.14159?

import mymodule

print(mymodule.pi)

In a nutshell…

Modules and importing are crucial concepts in Python, enabling you to build modular and maintainable code. They help you reuse code efficiently, manage large projects effectively, and collaborate with others on Python projects.