נבנה על ידיBuilt by Ariel Yohimovich
import random ואז random.choiceimport random then random.choicefrom math import sqrtfrom math import sqrtimport datetime as dtimport datetime as dtfrom, או לתת לו כינוי קצר עם as.import loads a module. Import the whole thing and use a dot, import just certain names with from, or give it a short alias with as.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.import ranodmModuleNotFoundError: 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.random.py, ואז ה-import טוען אותו במקום המקורי. כאן מודגם השם השגוי עצמו.Naming your own file like a standard module, e.g. random.py, so import loads yours instead of the real one. Here the wrong name itself is shown.datetime מחשב תאריכים, ו-Counter סופר בשורה אחת מה שבנינו במפגש 8 עם מילון.Python has a huge standard library. Before writing something yourself, check whether it exists. datetime does date maths, and Counter counts in one line what we built in session 8 with a dict.from datetime import date
print(date(2026, 11, 19) + 21)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).timedelta.Adding a number to a date. You need timedelta.import books טוען את books.pyimport books loads books.pybooks.py עם פונקציות, קובץ אחר יכול לכתוב import books ולהשתמש בהן. ככה מפצלים פרויקט גדול לקבצים לפי נושא. הדוגמה כותבת את המודול בעצמה כדי שאפשר יהיה להריץ אותה בדפדפן, ולכן קוראת ל-importlib.invalidate_caches(): פייתון זוכרת אילו קבצים היו בתיקייה כשהתוכנית התחילה, והשורה הזו אומרת לה לבדוק שוב. ב-VS Code פשוט יוצרים את books.py כקובץ רגיל, ואז אין צורך בה.Every .py file is a module. If a folder has books.py with functions, another file can import books and use them. That's how a big project is split into files by topic. This example writes the module itself so it runs in the browser too, which is why it calls importlib.invalidate_caches(): Python remembers which files were in the folder when the program started, and this line tells it to look again. In VS Code you simply create books.py as a normal file and don't need it.__name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directlydef 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__)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__)📚 Dune, Frank Herbert הקובץ רץ ישירות __main__
📚 Dune, Frank Herbert run directly __main__
python -m pip install requestspython -m pip install requestspip list מראה מה מותקןpip list shows what's installedrequirements.txt לשיתוףrequirements.txt for sharingpython -m pip (ב-Mac python3 -m pip) כדי להיות בטוחים שההתקנה נכנסת ל-Python הנכון.A package is code someone published online (on PyPI). pip downloads and installs it. Write python -m pip (Mac: python3 -m pip) to be sure it installs into the right Python.import not_installed_package_xyzModuleNotFoundError: 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.python3 -m venv .venv יוצרpython3 -m venv .venv creates.venv) עם Python וחבילות משלה. כל פרויקט מקבל סביבה משלו, כך שגרסה של חבילה בפרויקט אחד לא שוברת פרויקט אחר. VS Code מזהה אותה ומציע לבחור בה.A virtual environment is a folder (usually .venv) with its own Python and packages. Each project gets its own, so a package version in one project can't break another. VS Code detects it and offers to use it.import random ואז random.choiceimport random then random.choicefrom math import sqrtfrom math import sqrtimport datetime as dtimport datetime as dtimport books טוען את books.pyimport books loads books.py__name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directlypython -m pip install requestspython -m pip install requestsimport random ואז random.choiceimport random then random.choice__name__ הוא "__main__" כשמריצים ישירות__name__ is "__main__" when run directlypython -m pip install requestspython -m pip install requestspython3 -m venv .venv יוצרpython3 -m venv .venv createsנבנה על ידיBuilt by Ariel Yohimovich