נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

מודולים, pip ו-venvModules, pip & venv

מפגש 13: import, מודולים משלך, הספרייה הסטנדרטית, התקנת חבילות וסביבה וירטואלית.Session 13: import, your own modules, the standard library, installing packages and virtual environments.

מה נעשה היוםToday's plan

  • importimport
  • stdlibstdlib
  • מודול משלכםYour own module
  • __main____main__
  • pippip
  • venvvenv
נושאTopic 1

importimport

import: להשתמש בקוד מוכןimport: using ready-made code

  • import random ואז random.choiceimport random then random.choice
  • from math import sqrtfrom math import sqrt
  • import datetime as dtimport datetime as dt

אנלוגיהAnalogy

מודול הוא כמו ספר מתכונים במדף: import מוריד את כל הספר, from ... import מצלם רק את המתכון שצריך.A module is like a cookbook on the shelf: import takes down the whole book, from ... import photocopies just the recipe you need.

טעות נפוצהCommon mistake

imports.py
import ranodm
מה Python עונהWhat Python says
ModuleNotFoundError: No module named 'ranodm'
ModuleNotFoundError: No module named 'ranodm': בודקים איות. ואם יש לכם קובץ בשם של מודול מוכר (random.py), משנים את שמו.ModuleNotFoundError: No module named 'ranodm': check the spelling. And if you have a file named like a real module (random.py), rename it.
נושאTopic 2

stdlibstdlib

הספרייה הסטנדרטיתThe standard library

  • datetime: תאריכים וזמניםdatetime: dates and times
  • collections: Counter ועודcollections: Counter and more
  • "Batteries included""Batteries included"

אנלוגיהAnalogy

הספרייה הסטנדרטית היא כמו ארגז כלים שמגיע עם הבית: מברגה, פטיש, מטר. לא צריך לקנות כלום כדי להתחיל.The standard library is like the toolbox that comes with the house: screwdriver, hammer, tape measure. Nothing to buy to get started.

טעות נפוצהCommon mistake

stdlib.py
from datetime import date
print(date(2026, 11, 19) + 21)
מה Python עונהWhat Python says
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int': כותבים + timedelta(days=21).TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int': write + timedelta(days=21).
נושאTopic 3

מודול משלכםYour own module

מודול משלכםYour own module

  • כל קובץ ‎.py הוא מודולEvery .py file is a module
  • import books טוען את books.pyimport books loads books.py
  • הקבצים באותה תיקייהFiles in the same folder

אנלוגיהAnalogy

לפצל לקבצים זה כמו לחלק ספר לפרקים: כל פרק על נושא אחד, ותוכן העניינים (import) מחבר ביניהם.Splitting into files is like dividing a book into chapters: one topic per chapter, and the table of contents (import) links them.
נושאTopic 4

__main____main__

if __name__ == "__main__"if __name__ == "__main__"

  • __name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directly
  • וכשמייבאים: שם המודולAnd when imported: the module's name
  • קוד הרצה נכנס לתנאיRun-only code goes inside the check
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

python
def card(title, author):
    return f"📚 {title}, {author}"

if __name__ == "__main__":
    print(card("Dune", "Frank Herbert"))
    print("הקובץ רץ ישירות", __name__)
def card(title, author):
    return f"📚 {title}, {author}"

if __name__ == "__main__":
    print(card("Dune", "Frank Herbert"))
    print("run directly", __name__)

    __main____main__

    books.py
    def card(title, author):
        return f"📚 {title}, {author}"
    
    if __name__ == "__main__":
        print(card("Dune", "Frank Herbert"))
        print("הקובץ רץ ישירות", __name__)
    def card(title, author):
        return f"📚 {title}, {author}"
    
    if __name__ == "__main__":
        print(card("Dune", "Frank Herbert"))
        print("run directly", __name__)
    פלטOutput
    📚 Dune, Frank Herbert
    הקובץ רץ ישירות __main__
    📚 Dune, Frank Herbert
    run directly __main__

    אנלוגיהAnalogy

    זה כמו שלט "להרצה ישירה בלבד" על כפתור הדגמה במכונה: כשמשתמשים במכונה כחלק ממפעל (import), הכפתור לא נלחץ.It's like a "direct use only" label on a machine's demo button: when the machine is part of a factory (import), the button isn't pressed.
    נושאTopic 5

    pippip

    pip: להתקין חבילותpip: installing packages

    • python -m pip install requestspython -m pip install requests
    • pip list מראה מה מותקןpip list shows what's installed
    • requirements.txt לשיתוףrequirements.txt for sharing

    אנלוגיהAnalogy

    pip הוא כמו חנות אפליקציות בטלפון: מחפשים בשם, לוחצים התקנה, והאפליקציה מוכנה לשימוש.pip is like a phone's app store: search by name, tap install, and it's ready to use.

    טעות נפוצהCommon mistake

    main.py
    import not_installed_package_xyz
    מה Python עונהWhat Python says
    ModuleNotFoundError: No module named 'not_installed_package_xyz'
    ModuleNotFoundError: מתקינים עם python3 -m pip install ..., ובודקים ב-VS Code שנבחר אותו מפרש.ModuleNotFoundError: install with python3 -m pip install ..., and check VS Code uses the same interpreter.
    נושאTopic 6

    venvvenv

    סביבה וירטואלית: venvVirtual environments: venv

    • python3 -m venv .venv יוצרpython3 -m venv .venv creates
    • activate מפעילactivate turns it on
    • חבילות לכל פרויקט בנפרדPackages per project

    אנלוגיהAnalogy

    venv הוא כמו קופסת אוכל נפרדת לכל ילד: מה שבקופסה של אחד לא מתערבב עם האוכל של השני.A venv is like a separate lunchbox for each child: what's in one box doesn't mix with the other's food.

    import random ואז random.choiceimport random then random.choice

    from math import sqrtfrom math import sqrt

    import datetime as dtimport datetime as dt

    datetime: תאריכים וזמניםdatetime: dates and times

    collections: Counter ועודcollections: Counter and more

    "Batteries included""Batteries included"

    כל קובץ ‎.py הוא מודולEvery .py file is a module

    import books טוען את books.pyimport books loads books.py

    הקבצים באותה תיקייהFiles in the same folder

    __name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directly

    וכשמייבאים: שם המודולAnd when imported: the module's name

    קוד הרצה נכנס לתנאיRun-only code goes inside the check

    python -m pip install requestspython -m pip install requests

    מה למדנוWhat we learned

    • import random ואז random.choiceimport random then random.choice
    • datetime: תאריכים וזמניםdatetime: dates and times
    • כל קובץ ‎.py הוא מודולEvery .py file is a module
    • __name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directly
    • python -m pip install requestspython -m pip install requests
    • python3 -m venv .venv יוצרpython3 -m venv .venv creates

    עכשיו לתרגולNow, practice

    5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
    הערות מרצהSpeaker notes

    נבנה על ידיBuilt by Ariel Yohimovich