14, అక్టోబర్ 2025, మంగళవారం

program no 45 in python.................

 To create a Python package containing two or more modules, follow these steps:

1. Create the Package Directory:
Create a directory that will serve as your package. This directory's name will be the package name. For example, create a directory named my_package.
2. Create the Modules:
Inside the my_package directory, create your Python modules (e.g., module1.py and module2.py).
my_package/module1.py:
Python
def greet(name):    return f"Hello, {name} from Module 1!"def add(a, b):    return a + b
my_package/module2.py:
Python
def farewell(name):    return f"Goodbye, {name} from Module 2!"def multiply(a, b):    return a * b
3. Create the __init__.py File:
Inside the my_package directory, create an empty file named __init__.pyThis file signifies to Python that the directory is a package. While it can be empty, it can also be used to define package-level imports or initialization code. 
my_package/__init__.py:
Python
# You can optionally import modules or functions here for easier access# from . import module1# from . import module2# from .module1 import greet, add# from .module2 import farewell, multiply
4. Using the Package:
Now you can use your package in another Python script. Create a file (e.g., main.py) outside the my_package directory.
main.py:
Python
# Import specific functions from modules within the packagefrom my_package.module1 import greet, addfrom my_package.module2 import farewell, multiplyprint(greet("Alice"))print(f"Sum: {add(5, 3)}")print(farewell("Bob"))print(f"Product: {multiply(4, 6)}")# You can also import the entire module and access its functions# import my_package.module1# print(my_package.module1.greet("Charlie"))
Directory Structure:
Code
.├── my_package/│   ├── __init__.py│   ├── module1.py│   └── module2.py└── main.py
When you run main.py, it will import and utilize the functions defined within module1.py and module2.py of your my_package.

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి