Intermediate·6 min·intermediate · modules
Modules & Imports
Modules & Imports
A module is just a .py file. A package is a folder with an __init__.py.
Four import shapes
import math— use asmath.piimport math as m— aliasfrom math import pi— pull a name into your namespacefrom math import pi as PI— pull and alias
Where Python looks
- Built-ins → 2. The current file's directory → 3. Installed site-packages → 4.
sys.path
Try it
- Try
from collections import defaultdict— build one. - Import
randomand generate 5 random ints between 1 and 100.