Python Package

Note (for context): A package in Python is a folder with an __init__.py file containing one or more modules. Packages help organize related modules.


1) Create a Simple Package

Create a folder myutils with an empty __init__.py. Inside it place a module math_ops.py. In math_ops.py define add(a, b) that returns a + b. In a separate script, import and test it.


2) Package with Multiple Modules

Extend myutils by adding another module str_ops.py. Define to_upper(s) that returns the uppercase version of string s. Import both modules and use their functions.


3) Nested Package

Create a folder myutils/tools (with __init__.py in both). Inside tools, add print_tools.py with a function say_hi(). Import and call it in a script.


4) Import Specific Function

From your package module math_ops, import only the add function using:

from myutils.math_ops import add

Call it.


5) Package Alias

Import your module with an alias:

Use it to call m.add().


6) Relative Import Inside Package

Inside str_ops.py, import to_upper from another module in the same package using relative import.


7) Package with Default Values

Add a function greet(name="User") in myutils/str_ops.py. Import and call it with and without an argument.


8) Create a Utility Package

Organize math functions (add, multiply, subtract) into a package named calc. Write a script that uses all of them.


9) Print Package Contents

Create a script that prints all function names in your package module.


10) Third-Party Package Use

Install the requests package (or any available package). Use it to fetch https://httpbin.org/get and print the status code.


11) Use datetime from datetime Package

Import datetime and print the current year.


12) Package with all

In your package’s __init__.py define __all__ listing modules to export. Import using:


13) Use collections Package

From the collections package, import Counter and use it to count characters in a string.


14) Package and Error Handling

Inside your package functions include exception handling. Call a function with incorrect input to trigger and handle the error.


15) Use os.path

From the os package’s submodule path, import exists() and check if a file exists.


16) Package with Subpackage

Create a subpackage math_tools inside myutils. Add a module power.py with a power() function. Import and call it.


17) Package with Default Print on Import

In your package’s __init__.py, add a print statement and observe what happens when you import the package.


18) Package Version Variable

Inside your package, define a variable __version__ = "1.0". Import it in a script and print it.


19) Use pathlib Package

Import pathlib.Path and use it to print the current working directory.


20) Create a Mini CLI Package

In your package, add a module cli.py with a function main() that prints:

Call it from a script.


🔍 Concepts Covered

Concept
Practice Included

Creating packages

#1, #3, #8

Module imports

#2, #4, #5

Aliasing

#5

Nested packages

#3, #16

Relative imports

#6

Third-party packages

#10, #13

Standard library packages

#11, #15, #19

__all__

#12

Package metadata

#18

Script / CLI usage

#20


🧠 Basic Package Structure


📝 Example Starter: math_ops.py


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated