Packages help organize your Python code into folders so it is easier to find and reuse.
Package structure and usage in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
package_name/
__init__.py
module1.py
module2.py
# To use a module from the package:
from package_name import module1
module1.some_function()The __init__.py file tells Python this folder is a package.
You import modules from the package using from package_name import module.
Examples
my_package with one module greetings.py. We call the say_hello function from it.Python
my_package/
__init__.py
greetings.py
# greetings.py content:
def say_hello():
print('Hello!')
# Using the package:
from my_package import greetings
greetings.say_hello()utils with two modules. We import both and use their functions.Python
utils/
__init__.py
math_tools.py
string_tools.py
# math_tools.py content:
def add(a, b):
return a + b
# string_tools.py content:
def shout(text):
return text.upper() + '!'
# Using the package:
from utils import math_tools, string_tools
print(math_tools.add(3, 4))
print(string_tools.shout('hi'))Sample Program
This program shows how to create a package my_package with a module greetings. Then it imports and uses the say_hello function.
Python
# Folder structure: # my_package/ # __init__.py # greetings.py # greetings.py content: def say_hello(): print('Hello from the package!') # main.py content: from my_package import greetings greetings.say_hello()
Important Notes
Every folder that should be a package needs an __init__.py file, even if it is empty.
You can import specific functions or classes from modules inside packages for cleaner code.
Packages help keep your project tidy and make code sharing easier.
Summary
Packages are folders with Python files and an __init__.py file.
Use packages to organize and reuse your code easily.
Import modules from packages using from package_name import module.
Practice
1. What is the main purpose of the
__init__.py file in a Python package?easy
Solution
Step 1: Understand the role of
The__init__.py__init__.pyfile tells Python that the directory should be treated as a package.Step 2: Differentiate from other options
It does not execute the main program, store global variables, or compile files; its main role is package identification.Final Answer:
To mark a directory as a Python package -> Option AQuick Check:
__init__.pymarks packages [OK]
Hint: Remember: __init__.py makes folder a package [OK]
Common Mistakes:
- Thinking __init__.py runs main code automatically
- Confusing it with a script file
- Assuming it compiles Python files
2. Which of the following is the correct way to import the module
utils from a package named mypackage?easy
Solution
Step 1: Recall Python import syntax for packages
To import a module from a package, usefrom package_name import module.Step 2: Match syntax to options
from mypackage import utils matches this syntax:from mypackage import utils.Final Answer:
from mypackage import utils -> Option DQuick Check:
Import module with 'from package import module' [OK]
Hint: Use 'from package import module' to import modules [OK]
Common Mistakes:
- Reversing package and module names
- Using incorrect import order
- Trying to import module as package
3. Given this package structure:
And this code:
If
mypackage/ __init__.py math_ops.py string_ops.py
And this code:
from mypackage import math_ops print(math_ops.add(2, 3))
If
math_ops.py contains a function def add(a, b): return a + b, what is the output?medium
Solution
Step 1: Understand the import and function call
The code importsmath_opsfrommypackageand callsmath_ops.add(2, 3).Step 2: Evaluate the function output
The functionaddreturns the sum of 2 and 3, which is 5.Final Answer:
5 -> Option CQuick Check:
2 + 3 = 5 [OK]
Hint: Trace function call and return value carefully [OK]
Common Mistakes:
- Forgetting to call the function with parentheses
- Confusing module and function names
- Assuming import fails without __init__.py content
4. You have a package folder named
But get
tools with modules calc.py and format.py. You try to run:from tools import calc print(calc.multiply(4, 5))
But get
ModuleNotFoundError. What is the most likely cause?medium
Solution
Step 1: Understand ModuleNotFoundError cause
This error often means Python does not recognize the folder as a package.Step 2: Check package structure requirements
Without__init__.py, Python won't treat 'tools' as a package, causing import failure.Final Answer:
Missing __init__.py file in the tools folder -> Option BQuick Check:
__init__.py missing causes ModuleNotFoundError [OK]
Hint: Always add __init__.py to package folders [OK]
Common Mistakes:
- Assuming function absence causes ModuleNotFoundError
- Ignoring package folder structure
- Blaming Python version without checking files
5. You want to create a package
Which code should you add to
datautils with submodules clean.py and transform.py. You want users to import clean_data function directly from datautils like this:from datautils import clean_data
Which code should you add to
datautils/__init__.py to enable this?hard
Solution
Step 1: Understand relative imports in packages
Inside__init__.py, use relative import with dot:from .clean import clean_data.Step 2: Confirm correct syntax for exposing functions
This syntax makesclean_dataavailable directly fromdatautils.Final Answer:
from .clean import clean_data -> Option AQuick Check:
Use 'from .module import func' in __init__.py [OK]
Hint: Use relative import with dot in __init__.py [OK]
Common Mistakes:
- Using absolute import without dot inside __init__.py
- Wrong import syntax like 'import clean_data from clean'
- Trying to import without __init__.py setup
