Loading...

Python Packages

A package is a folder of modules. math was one module, one file. bakery holds more than one file, and that folder is the package. This page does not install anything, and it does not open a data file. You still import a name. The difference is that the name now points at a folder. The folder's name is the name you import. Each file inside keeps its own name.

the module prints nothing

python
def heat():
    return "warm"

This file is bakery/oven.py. heat returns the word warm and does not print it. Run this file alone and the screen stays empty. The call that prints warm lives in another file. A function you never call does not write anything.

📊 The folder and the import

Piece What it does
bakery/__init__.py an empty file; it marks the folder as a package
bakery/oven.py heat returns warm and prints nothing by itself
from bakery import oven a dot joins the folder and the module

the call lives in another file

python
from bakery import oven
print(oven.heat())

The screen shows warm. The dot joins bakery and oven. A slash in that spot is not an import, and the program stops before it runs. The name of that stop is SyntaxError. That slash line is not in this program. init.py can stay empty. You do not put heat in it on this page. None is not written when the slash stops the program. oven.heat() is the call. heat is not a loose name in the second file. The import gives you oven, and heat stays on oven.

The next page is the library that comes with the language. This page only groups modules in one folder.

A folder of modules

oven.py alone shows nothing. The other file shows warm. The dot joins bakery and oven. init.py can be empty. The next page is the library that comes with the language.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top