Chapter 15 of 15All chapters
Chapter 15 of 15
Modules and packages
Splitting code up and pulling other code in.
Importing
Every .py file is a module. import math brings in the whole module, and from math import sqrt brings in one name. A package is a directory of modules.
- Import at the top of the file, standard library first, then third party, then your own.
- if __name__ == "__main__": marks code that should run only when the file is the entry point.
The standard library and pip
Python ships with a large standard library: json, datetime, pathlib, re, random and more. Anything else installs with pip, and a virtual environment keeps each project's packages separate.