The use of modules and packets in Python: the organization of code and re -use of functions and classes.

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A self-study guide for Python 3 compiled from the materials on this site. Primarily intended for those who want to learn the Python programming language from scratch.

Working with modules and packages in Python: a complete guide

Working with modules and packages in Python is a fundamental concept that allows you to organize code into logically linked blocks for reuse and to ensure the purity and structure of the project. Understanding these mechanisms is critically important for developing high-quality Python applications.

What are modules in Python

The module This is a file with the .py extension containing Python code. It can include definitions of functions, classes, variables, and executable instructions. Modules solve the problem of code organization and avoid duplication of functions in different parts of the program.

Creating and importing a module

Create a filecalculator.py :

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

PI = 3.14159

Import the module in another file:

import calculator

result = calculator.add(5, 3)
print(result)  # 8
print(calculator.PI)  # 3.14159

Packages in Python: structured organization

Package This is a directory containing one or more modules and the required __ file.init__.py . This file tells Python that the directory should be treated as a package. Packages create a hierarchical structure of modules.

Package Structure

mypackage/
    __init__.py
    module1.py
    module2.py
    subpackage/
        __init__.py
        module3.py

Import from package

import mypackage.module1
from mypackage import module2
from mypackage.subpackage import module3

Python Standard Library

Python comes with an extensive standard library that includes modules for working with files, the network, mathematical calculations, dates, and more. These modules are available without installing additional packages.

 

Popular modules of the standard library

import os
import sys
import json
import datetime
import math
import random

# Working with
the print file system(os.listdir('.'))  # List of files in the current
print directory(os.getcwd()) # Current working directory

# Working with JSON
data = {"name": "Python", "version": "3.9"}
json_string = json.dumps(data)

# Mathematical operations
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793

Ways to import modules

1. Basic import

import module_name

2. Importing with an alias

Use aliases to make it easier to work with modules with long names:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

3. Importing specific names

Import only the necessary functions or classes.:

from math import sqrt, pi, sin, cos
from datetime import datetime, timedelta

4. Importing all content (not recommended)

from module_name import *

Warning: This method is not recommended, as it may lead to name conflicts and pollute the global namespace.

Managing module search paths

Using sys.path

import sys
sys.path.append('/path/to/custom/modules')
import custom_module

PYTHONPATH environment variable

export PYTHONPATH="/path/to/modules:$PYTHONPATH"

Using setuptools

For professional development, use setup.py :

from setuptools import setup, find_packages

setup(
    name="myproject",
    version="1.0.0",
    packages=find_packages(),
    install_requires=[
        "requests",
        "numpy",
    ],
)

File init.py and its role

File __init__.py performs several important functions:

  1. Package marker: Indicates to Python that the directory is a package
  2. Package initialization: The code is executed the first time the package is imported
  3. Import Management: Defines what is available when importing a package

Example init.py

# mypackage/__init__.py
from .module1 import important_function
from .module2 import UsefulClass

__version__ = "1.0.0"
__all__ = ["important_function", "UsefulClass"]

Conditional import and error handling

try:
    import numpy as np
    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False
print("NumPy is not installed")

if HAS_NUMPY:
    # Code using NumPy
pass

Best practices for working with modules

  1. Use clear names: Choose clear names for modules and packages
  2. Follow PEP 8: Follow Python naming standards
  3. Document modules: Add docstrings to the beginning of modules
  4. Avoid cyclic imports: Design an architecture that eliminates mutual dependencies
  5. Use virtual environments: Isolate project dependencies

Creating custom packages

Project structure

myproject/
    setup.py
    README.md
    mypackage/
        __init__.py
        core.py
        utils.py
        tests/
            __init__.py
            test_core.py

Package Installation

pip install -e .  # Install in
the pip install development mode.     # Normal installation

Conclusion

Working with modules and packages in Python is the foundation for creating scalable and maintainable applications. Proper code organization using modules and packages greatly simplifies project development, testing, and maintenance. Mastering these concepts is a necessary step for any Python developer.

 

 

categories

  • Introduction to Python
  • Python Programming Basics
  • Control Structures
  • Data Structures
  • Functions and Modules
  • Exception Handling
  • Working with Files and Streams
  • File System
  • Object-Oriented Programming (OOP)
  • Regular Expressions
  • Additional Topics
  • General Python Base