0
0
Pythonprogramming~30 mins

Package structure and usage in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Package structure and usage
📖 Scenario: You are organizing a small Python project into packages to keep your code clean and easy to use. You will create a package with modules and use them in a main script.
🎯 Goal: Build a simple Python package with two modules and use functions from those modules in a main script.
📋 What You'll Learn
Create a package folder named mypackage with an __init__.py file
Create two modules inside mypackage: module1.py and module2.py
Write one function in each module: greet() in module1.py and farewell() in module2.py
Import and use these functions in a main script main.py
💡 Why This Matters
🌍 Real World
Organizing code into packages helps keep projects clean and makes code reusable across different programs.
💼 Career
Understanding package structure is essential for working on larger Python projects and collaborating with other developers.
Progress0 / 4 steps
1
Create package folder and modules
Create a folder named mypackage. Inside it, create an empty file named __init__.py. Then create two files inside mypackage: module1.py and module2.py. In module1.py, write a function called greet that returns the string 'Hello from module1'. In module2.py, write a function called farewell that returns the string 'Goodbye from module2'.
Python
Need a hint?

Remember, __init__.py can be empty but it tells Python this folder is a package.

2
Create main script and import functions
Create a file named main.py outside the mypackage folder. In main.py, import the greet function from mypackage.module1 and the farewell function from mypackage.module2.
Python
Need a hint?

Use the syntax from package.module import function to import.

3
Use imported functions in main.py
In main.py, call the greet() function and store its result in a variable named hello_message. Call the farewell() function and store its result in a variable named bye_message.
Python
Need a hint?

Just call the functions and assign their return values to variables.

4
Print the messages
In main.py, print the variables hello_message and bye_message on separate lines.
Python
Need a hint?

Use two print statements, one for each message.