3.6 KiB
3.6 KiB
06 – Packages and Modules in Python
This document explains how modules, packages, and the __name__ concept work in Python.
These features help organize code, reuse functionality, and build scalable projects.
1. Importing External Modules
Python allows you to import external libraries installed in your environment.
Example: Using the emoji Module
Code
import emoji
print(emoji.emojize("abbas is :red_heart:"))
Explanation
import emojiimports the entireemojimodule.emoji.emojize()converts emoji aliases into actual emoji characters.- You must use the module name (
emoji) to access its functions.
Importing a Specific Function
Code
from emoji import emojize
print(emojize("abbas is :red_heart:"))
Explanation
from emoji import emojizeimports only theemojizefunction.- You can call the function directly without prefixing the module name.
- This approach is cleaner when you only need a specific function.
2. Creating a Module
A module is a single Python file containing functions, classes, or variables.
File Structure
hi.py
main.py
hi.py
def hi():
print("Hi :)")
main.py
import hi
hi.hi()
Explanation
hi.pyis a module.hi()is a function defined inside the module.import hiloads the module.hi.hi()calls the function from the module.
3. Creating a Package
A package is a directory that contains multiple modules.
Package Structure
honor/
│── __init__.py
│── hi.py
main.py
honor/hi.py
def hello():
print("Hi :)")
honor/__init__.py
Explanation
- The
honordirectory is a package. __init__.pytells Python that this directory is a package.- The file can be empty, but it must exist (especially for older Python versions and clarity).
Importing from a Package (Method 1)
main.py
from honor import hi
hi.hello()
Explanation
- Imports the
himodule from thehonorpackage. - Accesses the function using
hi.hello().
Importing from a Package (Method 2)
main.py
from honor.hi import hello
hello()
Explanation
- Imports the
hellofunction directly. - Allows calling the function without the module name.
- Cleaner when only one function is needed.
4. The __name__ Concept
Every Python file has a built-in variable called __name__.
Code
print(__name__)
Behavior
When a File Is Run Directly
python3 abbas.py
Output:
__main__
- This means the file is the entry point of the program.
When a File Is Imported
import abbas
Output:
abbas
__name__is set to the module name, not__main__.
5. Why __name__ == "__main__" Is Important
This pattern allows code to run only when the file is executed directly, not when imported.
Example
def main():
print("Running directly")
if __name__ == "__main__":
main()
Explanation
- The code inside the
ifblock runs only when the file is executed directly. - Prevents unwanted execution when the file is imported as a module.
- This is a standard Python best practice.
Summary
- Module: A single
.pyfile - Package: A directory containing modules
__init__.py: Marks a directory as a packageimport module: Imports the whole modulefrom module import item: Imports specific items__name__: Identifies how a file is executed