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)

Python package: folders that hold multiple modules

In Python, a package is a directory that contains multiple modules and subpackages, allowing you to organize your code in a hierarchical manner. By grouping related modules and subpackages together, you can create more maintainable, readable, and scalable code.

Creating a Package with Multiple Modules

To create a package with multiple modules, follow these steps:

  1. Create a new directory for the package, and give it a descriptive name.

  2. Inside the package directory, create an empty file named __init__.py. This file is used to mark the directory as a Python package. The __init__.py file can also contain package-level code, such as package-level variables, functions, or class definitions.

  3. Add modules to the package by creating Python files (with a .py extension) inside the package directory.

Example: Creating a Package with Multiple Modules

Let's create a package called my_package with the following structure:

my_package/
������ __init__.py
������ module_a.py
������ module_b.py
  1. Create a new directory named my_package.

  2. Inside my_package, create an empty __init__.py file.

  3. Add code to the module_a.py and module_b.py files:

# In module_a.py
def func_a():
    return "Hello from func_a in module_a!"

# In module_b.py
def func_b():
    return "Hello from func_b in module_b!"

Importing Modules from a Package

To use the modules and functions in a package, you can use the import statement followed by the package name, the module name, and the function or class you want to import.

For example, create a script called main.py and import the functions from the my_package:

# In main.py
from my_package.module_a import func_a
from my_package.module_b import func_b

print(func_a())  # Output: Hello from func_a in module_a!
print(func_b())  # Output: Hello from func_b in module_b!

In conclusion, a Python package is a directory that contains multiple modules and subpackages, allowing you to organize your code in a structured way. By using packages, you can improve the maintainability, readability, and scalability of your code.

  1. Creating a package in Python with multiple modules:

    • Description: A package is created by having a directory with an __init__.py file. Multiple modules can reside within this package directory.
    • Code: (Directory structure)
      my_package/
      ������ __init__.py
      ������ module1.py
      ������ module2.py
      
  2. Importing modules from a package in Python:

    • Description: Use the import statement to import modules from a package. Specify the package and module names using dot notation.
    • Code:
      # Importing a module from a package
      from my_package import module1
      
      result = module1.some_function()
      print(result)
      
  3. Nested packages and submodules in Python:

    • Description: Packages can have subpackages, forming a nested structure. Use dot notation to import modules from nested packages.
    • Code: (Directory structure)
      my_package/
      ������ __init__.py
      ������ module1.py
      ������ submodule/
      ��   ������ __init__.py
      ��   ������ module3.py
      
  4. Managing dependencies within a Python package:

    • Description: Specify dependencies in setup.py or pyproject.toml. Use tools like pip for dependency management and installation.
    • Code: (setup.py)
      from setuptools import setup, find_packages
      
      setup(
          name='my_package',
          version='0.1',
          packages=find_packages(),
          install_requires=[
              'dependency1',
              'dependency2',
          ],
      )