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 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:
Create a new directory for the package, and give it a descriptive name.
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.
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
Create a new directory named my_package
.
Inside my_package
, create an empty __init__.py
file.
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.
Creating a package in Python with multiple modules:
__init__.py
file. Multiple modules can reside within this package directory.my_package/ ������ __init__.py ������ module1.py ������ module2.py
Importing modules from a package in Python:
import
statement to import modules from a package. Specify the package and module names using dot notation.# Importing a module from a package from my_package import module1 result = module1.some_function() print(result)
Nested packages and submodules in Python:
my_package/ ������ __init__.py ������ module1.py ������ submodule/ �� ������ __init__.py �� ������ module3.py
Managing dependencies within a Python package:
setup.py
or pyproject.toml
. Use tools like pip
for dependency management and installation.from setuptools import setup, find_packages setup( name='my_package', version='0.1', packages=find_packages(), install_requires=[ 'dependency1', 'dependency2', ], )